Here im trying to sort the words in a string that has starting and ending letter as same . it should be sorted in place, other words are left undisturbed.
import java.util.*;
public class MyClass {
public static void main(String args[]) {
String arr[]={"zpppz","poop","zllo","bob","yea"};
Arrays.sort(arr , new Comparator<String>(){
public int compare(String a ,String b){
if((a.charAt(0) == a.charAt(a.length()-1 ) )&&(b.charAt(0)==b.charAt(b.length()-1 ) ) ){
return a.compareTo(b);
}
return 0;
}
} );
for(String s: arr ) System.out.println(s);
}
}
expected output: "bob" "poop" "zllo" "zpppz" "yea" but im getting output as: "bob" "poop" "zpppz" "zllo" "yea"
bob,poopandzpppz. According to your criteria, the order you are getting is the correct order.zllocome beforezpppzin your expected output? Am I missing something? iszthe same letter aso?zpppz,zllo,poop,yea,bobthen your sorted list would bebob,zllo,poop,yea,zpppz, correct?