Here you can find the source of sortMapByValue(Map
public static <K, V extends Comparable<V>> Map<K, V> sortMapByValue(Map<K, V> input, final boolean desc)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { public static <K, V extends Comparable<V>> Map<K, V> sortMapByValue(Map<K, V> input, final boolean desc) { LinkedHashMap<K, V> output = new LinkedHashMap<K, V>(input.size()); ArrayList<Map.Entry<K, V>> entryList = new ArrayList<Map.Entry<K, V>>(input.size()); Collections.sort(entryList, new Comparator<Map.Entry<K, V>>() { public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { if (desc) return o2.getValue().compareTo(o1.getValue()); return o1.getValue().compareTo(o2.getValue()); }//from w w w . jav a2 s .co m }); for (Map.Entry<K, V> entry : entryList) { output.put(entry.getKey(), entry.getValue()); } return output; } public static <K, V extends Comparable<V>> Map<K, V> sortMapByValue(Map<K, V> input) { return sortMapByValue(input, true); } }