[My Blog]
All roads lead to JavaRanch
Abhineet Kapil wrote:There is a limitation with Comparable interface . ie out of 4 properties of DVDInfo Class you can sort it by one property only...
Really? Nothing in the compareTo() method javadoc says anything about only being able to compare on one property. It seems to me that would be a matter of how you implement compareTo().
[My Blog]
All roads lead to JavaRanch
There is a limitation with Comparable interface . ie out of 4 properties of DVDInfo Class you can sort it by one property only...
Abhineet Kapil wrote:The point is that you can override int compareTo(Object o) method only once in a class that implements Comparable interface...
So you can do it for any one of the property...
It doesn't matter that the compareTo method is called only once.
To sort on multiple properties, do something like this in your compareTo method:
1. Compare the primary properties you want to sort on.
2. If they are not equal, then return the appropriate value (-1 or 1).
3. If they are equal, then compare the secondary properties you want to sort on.
4. If they are not equal, then return the appropriate value (-1 or 1).
5. If they are equal, then compare the ternary properties you want to sort on.
...
6. If all properties are equal, return 0.
David Newton
Today 15:23:40 Subject: Sorting arraylist on the basis of object
Then you probably meant to say "you can only implement a single sort order using Comparable". (Which is also true only if your compareTo doesn't do any logic to decide how to sort.)