Here you can find the source of sortByValue(Map map, boolean reverse)
public static Map sortByValue(Map map, boolean reverse)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { public static Map sortByValue(Map map, boolean reverse) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Comparator() { @Override//from w w w . j av a 2s. co m public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()); } }); if (reverse) { Collections.reverse(list); } Map result = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); result.put(entry.getKey(), entry.getValue()); } return result; } }