aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compare() method of Comparator 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 "compare() method of Comparator" Watch "compare() method of Comparator" New topic
Author

compare() method of Comparator

sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Hello,
I have trying this code. (K&B SCJP5 Generics & Collection)
Can somebody please explain me what is going on in the LINES 1,2,3,4 marked in the following code:


What is happening when LINE 3 is invoked? I have tried to understand the Comparator classes compare method but the arguments keep bluffing me? When the compare() is called how does it get the two arguments? It's been very confusing for me so I seek an explanation.

This is just a question so feel free to answer.

[ December 13, 2008: Message edited by: Sudipto Shekhar ]

Regards, Sud.
SCJP 5 ScjpFAQ JLS
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

can you please Quote Your Sources...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

I am sorry for the inconvenience:

I have taken the example from K&B SCJP 5 Book Generics and Collections chapter.

Thank you.
Lidija Sketa
Greenhorn

Joined: Nov 12, 2008
Posts: 26
I had quite some problems with understanding how Comparator and Comparable work. It is not so complicated though, as long as you remember what the meaning of the compare / compareTo method return value is.

From Java 2 API Comparator.compare:
"Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second."


This means if you call compare with two strings, ie:


s1 is less than s2 and the method compare returns negative integer. This means that s1 is less than s2.
So the order of the strings will be: "abc", "def"

In your case:


The same principle applies: if fst is less than sec, negative integer is returned.
sec.compareTo(fst) works, because String implements Comparable.

From Java 2 API (Comparable.compareTo):
"The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true."

So if we call "def".compareTo("abc"), this returns > 0.

Your compare method returns positive integer, which means that the first argument is greater than the second. This means that the strings will be sorted in descending order: "def", "abc".

Hope this makes sense.
[ December 13, 2008: Message edited by: Lidija Sketa ]
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 165

public int compare(String fst,String sec){ //LINE 3
return sec.compareTo(fst); // LINE 4
}

The compare method is implemented to get reverse sorting. How?
If fst is greater than sec( eg: if fst= "def" and sec ="abc"),
then the compareTo() method would return a negative number.
And hence the compare method would return a negative number.. implying that fst is smaller than sec( i.e. fst should come before sec).So "def" would come before "abc".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compare() method of Comparator