I have a String
array:
String[] str = {"ab" , "fog", "dog", "car", "bed"};
Arrays.sort(str);
System.out.println(Arrays.toString(str));
If I use Arrays.sort
, the output is:
[ab, bed, car, dog, fog]
But I need to implement the following ordering:
FCBWHJLOAQUXMPVINTKGZERDYS
I think I need to implement Comparator
and override compare
method:
Arrays.sort(str, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
return 0;
}
});
How should I go about solving this?