This week's book giveaway is in the Agile forum.
We're giving away four copies of The Software Craftsman and have Sandro Mancuso on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes comparator problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "comparator problem" Watch "comparator problem" New topic

comparator problem

sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
this question is from Master Exam, Test prep software

import java.util.*;
class comp2
public static void main(String []args)
String words[] = {"Good","Bad","Ugly"};
Comparator<String> best = new Comparator<String>(){
public int compare(String s1, String s2)
return s2.charAt(1)-s1.charAt(1);//what is happening here?
Arrays.sort(words, best);

what is happening in the question? please help
thanks in advance
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

here you are comparing in reverse order. String class charAt() method will find the character value in specified location.remember charater is also integer value only, so you can do like
int i= 'a'-'a';

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
VidyaVikas Yalamanchili

Joined: Nov 26, 2007
Posts: 10

In the above code compare should always return a int.When you operate on characters every character can convert to an int.

you can give int i='a'

which will get i value is the ascii for a.So when you do s.charAt[1]-s.charAt[1] the sort happens on the second character of the string.

with Regards,<br />VidyaVikas Yalamanchili SCJP 5.0
sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
i get a little bit.
so is it doing like this:-
s2.charAt(1)="Ugly" - s1.charAt(1)="Bad"

please explain
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
by Sweety
so is it doing like this:-
s2.charAt(1)="Ugly" - s1.charAt(1)="Bad"

Try adding the bold line below you will get to know.

The output of this is as-:
g a
o a
o g

[ September 15, 2008: Message edited by: Pranav Bhatt ]
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3325

Basically it is doing a revers sort based on the second character of each word. charAt is 0 based. so 1 means second character and 0 means first character.

The comparator method returns 0 if 2 objects are equal and a -ve value if s1 < s2 and +ve value if s1 > s2. For example if you have Bad as s1 & Ugly as s2

s2.charAt(1) = 'g' ascii value is 103
s1.charAt(1) ='a' ascii value is 97

check -->

s2.charAt(1) - s1.charAt(1) = 6 is a positive number. Since it is a + ve number s1 > s2 that means Bad > Ugly. So you get this sorted as

Good --> Ugly --> Bad

[500+ Java Interview Questions and Answers ] | [Java job hunting know how] | [Java resumes]
sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
thanks for your replies
subject: comparator problem