Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Second order sorting using Comparable Interface

 
sudha swami
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps something like this. Sorts first by height, and then by name.


Output:
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sudha swami
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic