• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Second order sorting using Comparable Interface

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps something like this. Sorts first by height, and then by name.


Output:
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic