I have a String array:
String[] str = {\"ab\" , \"fog\", \"dog\", \"car\", \"bed\"};
Arrays.sort(str);
System.out.println(Arrays.toString(str
final String ORDER= "FCBWHJLOAQUXMPVINTKGZERDYS";
Arrays.sort(str, new Comparator() {
@Override
public int compare(String o1, String o2) {
return ORDER.indexOf(o1) - ORDER.indexOf(o2) ;
}
});
You can also add:
o1.toUpperCase()
If your array is case in-sensitive.
Apparently the OP wants to compare not only letters but strings of letters, so it's a bit more complicated:
public int compare(String o1, String o2) {
int pos1 = 0;
int pos2 = 0;
for (int i = 0; i < Math.min(o1.length(), o2.length()) && pos1 == pos2; i++) {
pos1 = ORDER.indexOf(o1.charAt(i));
pos2 = ORDER.indexOf(o2.charAt(i));
}
if (pos1 == pos2 && o1.length() != o2.length()) {
return o1.length() - o2.length();
}
return pos1 - pos2 ;
}