Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
SCJP 1.2
Originally posted by Frank Jacobsen:
NO !
Frank
SCJP 1.2
42
Hmmm. Best not use this as a general template.Originally posted by Jeroen Wenting:
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Oh, you most certainly can, and even simpler than you thought because the JVM will do the ClassCastException for you:Sorry, you'll have to forgive me my preference for covariant compareTo() and equals() methods; I find them easier on the eye and they may save you a couple of CPU cycles as well. If you need to sort in multiple ways, you're better off writing a Comparator:Now if "sortme" can be null things start to get a bit more elaborate. Keep in mind the contract for Comparator, specifically that sgn(compare(x, y)) == -sgn(compare(y, x)) even if x and/or y are null. I would not recommend coding this yourself; rather, use the Commons Collections ComparatorUtils. The nullHighComparator() and nullLowComparator() calls will decorate any existing Comparator that doesn't handle nulls to make it correctly handle null values. There's also a reversedComparator() in thereOriginally posted by KR Campbell:
Why can't he just wrap the compareTo() method of his String sortMe field?
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
If it gets out of hand, the Commons Collections (see reference above) has some neat utility classes to chain comparators like this.Originally posted by Stan James:
[...] I recently had one like this to sort on multiple fields: [...]
Really? I regularly find reason to do this. For example, in my current project there's a Message type that's Comparable (there are Comparators for different sorting orders as well). There are a number of different Message subtypes, but the subtype doesn't really affect the way the messages are sorted. It was not hard to make this behave consistently, to the contrary: there's a straightforward implementation of compareTo in Message which works regardless of the subtype.BTW: Somebody mentioned supporting Liskov Substitution by comparing to subtypes. In general, it's pretty hard to make [...] compareTo() behave consistently between any two different types in a way that makes any sense. [...] I doubt I'd try it.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ah, now I see where our understanding is diverging.Originally posted by Stan James:
Your example of sorting unlike things sounds pretty reasonable at first blush. But you could run into unpredictable results if compareTo says two objects are equal while hashcode or equals does not.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I really don't think we disagree on that one Stan... or that I ever suggested it.Originally posted by Stan James:
I think I'll stick to my guns on this one. If onePoundApple.equals(onePoundOrange) things are just not right in the world. [...] It's very tempting to cheat on compareTo() just for sorting by weight and say I'll never compare Apples to Oranges, but it makes Apple and Orange a very long debugging session waiting to happen in the next release.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
But Stan, you can't simply deny reality. I gave you a real life example of a class hierarchy that implements a natural order across different sub types, in a completely consistent fashion with equals() and hashCode(). Please point out where it doesn't satisfy the contracts, or where it doesn't make sense.Originally posted by Stan James:
[...] I'd suggest compareTo should fail if this and the argument object are not exactly the same class. instanceof and casting are not even sufficient with that rule.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
This would be dangerous to use as the natural sorting order because of the inconsistency with equals(). With Comparators, an inconsistency with equals() tends to cause much less confusion, although you should still be wary against using Comparator like that to construct, say, a TreeSet. Your example is about Comparators, so there is basically no problem for the cautious developerOriginally posted by KR Campbell:
[...] where does this leave me if I want to compare a set of cars based on their top speed? [...]
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
That's an enlightening remark, and I think this assumption is underneath much of the discussion above. Two remarks.Originally posted by Stan James:
[...] It seems dangerous to have two objects compare as equal unless all their attributes match. [...]
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|