Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparator Interface

 
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
Marshal
Pie
Posts: 21413
84
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic