• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting and Comparators

 
Ida Achi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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




 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Ida Achi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry that was a typo.
 
brijesh tr
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brijesh please check your private messages for some administrative matter...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic