wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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: 9291
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Sorting and Comparators
 
Similar Threads
Comparator Doubt
Compare() - from Master Exam
Really Confused about using the Comparator Interface in Collections
Sorting order
Sorting