my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sorting and Comparators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sorting and Comparators" Watch "Sorting and Comparators" New topic
Author

Sorting and Comparators

Ida Achi
Greenhorn

Joined: Oct 02, 2009
Posts: 25

I am not quite getting this. Please explain to me why "Good" is the answer in below. Thanks




Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
it is sorted in reverse alphabetical order on the second alphabet in each word, which are 'o' , 'b' and 'g' and hence word with 'o' as second letter will come first which is good.

can you please quote the source???


SCJP 1.6 96%
Ida Achi
Greenhorn

Joined: Oct 02, 2009
Posts: 25
Sorry, the source is the K and B scjp6 Master Exam.


it is sorted in reverse alphabetical order on the second alphabet in each word, which are 'o' , 'b' and 'g' and hence word with 'o' as second letter will come first which is good.


if that is the case why arent the second alphabet in each word not 'o', 'a' and 'g'???
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
sorry that was a typo.
brijesh tr
Greenhorn

Joined: Mar 13, 2008
Posts: 1
import java.util.*;
public class bktbean {
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 s1.charAt(1)- s2.charAt(1);
}

};
Arrays.sort(words,best);
System.out.println(words[0]);

}
}


it is sorted in reverse alphabetical order on the second alphabet in each word, which are 'o' , 'a' and 'g' and hence word with 'o' as second letter will come first ,which is good.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Brijesh please check your private messages for some administrative matter...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting and Comparators
 
Similar Threads
Really Confused about using the Comparator Interface in Collections
Compare() - from Master Exam
Sorting order
Comparator Doubt
Sorting