• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparator

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have difficulty in remembering the order of subtraction in Comparator implementation.



If we want to sort in descending order, we should write i2-i1 or i1-i2?
Normally, it's very easy to test with computer but during actual interviews we cannot test, we have to write code on the paper!

Does anyone have a trick to remember this order?

Thanks.
 
Ranch Hand
Posts: 1283
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
both will work
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Netbeans IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
firstarg-secondarg will give you result in ascending order
while secondarg -firstarg will give you result in descending order
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swerrgy Smith wrote:Does anyone have a trick to remember this order?


It's very simple, and it's explained in the API documentation of java.util.Comparator.compare().

Suppose you want to order the numbers in ascending order. compare() should then return a negative number if i1 is less than i2, 0 if they are equal, and a positive number if i1 is greater than i2.

Now, think about what you get when you do i1 - i2 or i2 - i1 with respect to this. Just try it in your head with concrete numbers, for example i1 = 3 and i2 = 5. What happens? Does this match with what should happen?
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Netbeans IDE Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By subtracting one salary from the other, the resulting value is automatically either negative, 0 or positive. Smart, right?

If you want to compare objects by more than one factor, start by comparing by the first factor (e.g first name). Then, if the first factors are equal, compare by the second factor (e.g. last name, or salary) etc.
 
reply
    Bookmark Topic Watch Topic
  • New Topic