Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare() method of Comparator

 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please Quote Your Sources...
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic