• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting and Comparators

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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




 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry that was a typo.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brijesh please check your private messages for some administrative matter...
 
reply
    Bookmark Topic Watch Topic
  • New Topic