I have a List<Map<String, String>>
. One particular value of the map is a numeric entry with decimals. I wish to sort the list in descending order based on that particular value of the map.
for example:
List<Map<String, String>> foo= new ArrayList<Map<String, String>>();
Map<String, String> bar1 = new HashMap<>();
Map<String, String> bar2 = new HashMap<>();
Map<String, String> bar3 = new HashMap<>();
bar1.put("name", "abc");
bar1.put("score", "72.5");
bar1.put("sex", "male");
foo.add(bar1);
bar2.put("name", "pqr");
bar2.put("score", "98.7");
bar2.put("sex", "female");
foo.add(bar2);
bar3.put("name", "xyz");
bar3.put("score", "100.0");
bar3.put("sex", "male");
foo.add(bar3);
.
.
.
.
and so on
I want to sort the List<Map<String, String>>
in descending order such that the map containing the score of 100.0 is on top.
I tried
Collections.sort( list, new Comparator<Map.Entry<K, V>>() {
@Override
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
but the "V" here is a string, while i need it to be sorted as a float. Any help would be appreciated.