Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparator Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparator Interface" Watch "Comparator Interface" New topic
Author

Comparator Interface

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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

Joined: Sep 28, 2004
Posts: 18526
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Comparator Interface
 
Similar Threads
Comparator Doubt
Compare() - from Master Exam
comparator problem
Can I sort String arrays?
Dount in K&B exam question