| Author |
Second order sorting using Comparable Interface
|
sudha swami
Ranch Hand
Joined: Apr 24, 2007
Posts: 177
|
|
Hi,
I have the following requirement. I need to do the sorting functionality. This the sql
select * from Person
order by name asc, time desc
This is the code
i dont know how to do second order sorting (time desc) using comparable interface.
THanks
Sudha
|
 |
Max Rahder
Ranch Hand
Joined: Nov 06, 2000
Posts: 177
|
|
Perhaps something like this. Sorts first by height, and then by name.
Output:
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Max Rahder wrote:
Just a word of caution: using simple subtraction for ints can get very nasty if the ints get quite big. Consider the case where h1.getHeight() is Integer.MIN_VALUE, and h2.getHeight() is positive. The result will overflow, and produce nasty results.
If the chances of this exist, the following is much safer (and also applies to long):
But if you know (and can guarantee) that the int values won't get that big, subtracting is not a problem.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
sudha swami
Ranch Hand
Joined: Apr 24, 2007
Posts: 177
|
|
Hi,
Thanks for the information. The above example has height in ascending order and the name is also in the ascending order.
My requirement is : Height should be in the ascending order and name should be in descending order.
Thanks
Sudha
|
 |
Max Rahder
Ranch Hand
Joined: Nov 06, 2000
Posts: 177
|
|
sudha swami wrote:Hi, Thanks for the information. The above example has height in ascending order and the name is also in the ascending order.
My requirement is : Height should be in the ascending order and name should be in descending order.
ThanksSudha
I just gave you an example. You'll have to figure out how to apply it to your situation. Experiment a little.
|
 |
sudha swami
Ranch Hand
Joined: Apr 24, 2007
Posts: 177
|
|
|
I tried it the same way as you did since last few days but i couldnt figure out whethere there is an option to do one column as ascending and the other one as descending
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
For descending, all you need to do is flip the sign (e.g. -13 becomes 13, or 481 becomes -481). You can just return -(previous return value) except when the previous return value is Integer.MIN_VALUE.
|
 |
sudha swami
Ranch Hand
Joined: Apr 24, 2007
Posts: 177
|
|
Rob,
I implemented the code changes according to your suggestion(return -(previous return value) ) and it is working fine.
Thanks for your suggestion. But i dont understand
"You can just return -(previous return value) except when the previous return value is Integer.MIN_VALUE "
Thanks
Sudha
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Integer.MIN_VALUE is -2,147,483,648, so -Integer.MIN_VALUE would be 2,147,483,648 - that is Integer.MAX_VALUE + 1. Since this is larger than Integer.MAX_VALUE, it wraps and... becomes Integer.MIN_VALUE again!
Byte.MIN_VALUE, Short.MIN_VALUE and Long.MIN_VALUE have the same issue.
|
 |
 |
|
|
subject: Second order sorting using Comparable Interface
|
|
|