I have an ArrayList of HashMap. Each HashMap contains many key-value-pairs. I want to sort the ArrayList by the value of the key distance in the HashMap.
ArrayList<HashMap<String, String>> arrayListHashMap =
new ArrayList<HashMap<String, String>>();
{
HashMap hashMap = new HashMap<String, Object>();
hashMap.put("key", "A key");
hashMap.put("value", "B value");
hashMap.put("distance", 2536);
arrayListHashMap.add(hashMap);
}
{
HashMap hashMap = new HashMap<String, String>();
hashMap.put("key", "B key");
hashMap.put("value", "A value");
hashMap.put("distance", 2539);
arrayListHashMap.add(hashMap);
}
You can try to sort with a custom comparator:
Note that I assumed all your values are strings which is not the case in your example (distance is an int value).