Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparator Interface  RSS feed

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: K& B Master Exam


Here the answer is Good,Ugly,Bad. How is it sorted??? Can anyone explain the flow of the program.I am getting confused with the compare() method.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here the answer is Good,Ugly,Bad. How is it sorted??? Can anyone explain the flow of the program.I am getting confused with the compare() method.


The purpose of the compare() method is to report the greater of two objects. With this method, an order can be established, and hence, the array can be sorted.

If you look at the compare method. It is reporting, the greater, based only on the second letter, and opposite of what the ASCII values are. Hence, the second letter is sorted in reverse order -- in this case, "o", "g", and "a".

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!