Here you can find the source of sortMapByValuesDescending( Map
public static <K, V extends Comparable> LinkedHashMap<K, V> sortMapByValuesDescending( Map<K, V> aMap)
//package com.java2s; //License from project: LGPL import java.util.*; import java.util.Map.Entry; public class Main { /**//ww w .jav a2 s . c om * Why the fuck do neither Java nor Guava have a Function to do this? */ public static <K, V extends Comparable> LinkedHashMap<K, V> sortMapByValuesDescending( Map<K, V> aMap) { List<Map.Entry<K, V>> tEntrySet = new LinkedList<Map.Entry<K, V>>( aMap.entrySet()); Collections.sort(tEntrySet, new Comparator<Map.Entry<K, V>>() { @Override public int compare(Entry<K, V> aValue1, Entry<K, V> aValue2) { return -aValue1.getValue().compareTo(aValue2.getValue()); } }); LinkedHashMap<K, V> rMap = new LinkedHashMap<K, V>(); for (Map.Entry<K, V> tEntry : tEntrySet) rMap.put(tEntry.getKey(), tEntry.getValue()); return rMap; } }