Here you can find the source of sortMap(Map oldMap)
public static Map sortMap(Map oldMap)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.Map; public class Main { public static Map sortMap(Map oldMap) { ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(oldMap.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { @Override/*from ww w . ja v a 2s .c o m*/ public int compare(Map.Entry<String, Integer> arg0, Map.Entry<String, Integer> arg1) { return arg0.getValue() - arg1.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; } }