This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting and Comparators  RSS feed

 
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: 9610
37
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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!