Here you can find the source of sortMap(Map oldMap, final boolean asc)
public static Map sortMap(Map oldMap, final boolean asc)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { public static Map sortMap(Map oldMap, final boolean asc) { if (null == oldMap) { return null; }//w w w .j av a 2s.co m ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(oldMap.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> arg0, Map.Entry<String, Integer> arg1) { if (asc) { return arg0.getValue() - arg1.getValue(); } else { return arg1.getValue() - arg0.getValue(); } } }); Map newMap = new LinkedHashMap(); for (int i = 0; i < list.size(); i++) { newMap.put(list.get(i).getKey(), list.get(i).getValue()); } return newMap; } }