I have list of maps and I want to sort the maps inside the list using the keys. As of now I am achieving this using the below Collections.sort method..
Collections.sort(listOfMaps, new Comparator<Map<String, String>>() {
@Override
public int compare(Map<String, String> o1, Map<String, String> o2) {
//return o1.get("cm_order_x").compareTo(o2.get("cm_order_x"));
String x1 = o1.get(Key1);
String x2 = o2.get(Key1);
String x3 = o1.get(Key2);
String x4 = o2.get(Key2);
int sComp = x1.compareTo(x2);
int sComp1 = x3.compareTo(x4);
if (sComp != 0) {
return sComp;
}
else if(sComp1 != 0) {
//return x3.compareTo(x4);
return sComp1;
}
else
{
String x5 = o1.get(Key3);
String x6 = o2.get(Key3);
return x5.compareTo(x6);
}
}
});
Is there any other better way to sort the list of maps in Java 8 ?
x1andx2are related, butx2andx3are not. I'd encourage you to not be afraid of "long" names:String key1FromO1 = o1.get(Key1), for instance.