//Map sorts according to value
public static Map<String, Integer> valueUpSort(Map<String, Integer> map) {
//map cannot be empty
if (map == null || map.isEmpty()) {
return null;
}
//Define a LinkedHashMap
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
//Comparator
Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
//Dimmersive order
return o2.getValue().compareTo(o1.getValue());
//Ascending order
// return ().compareTo(());
}
});
Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();
Map.Entry<String, Integer> tmpEntry = null;
while (iter.hasNext()) {
tmpEntry = iter.next();
sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
}
return sortedMap;
}