Here you can find the source of sortHashMapByValues(HashMap
public static LinkedHashMap<String, Integer> sortHashMapByValues(HashMap<String, Integer> passedMap)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; public class Main { public static LinkedHashMap<String, Integer> sortHashMapByValues(HashMap<String, Integer> passedMap) { List<String> mapKeys = new ArrayList<String>(passedMap.keySet()); List<Integer> mapValues = new ArrayList<Integer>(passedMap.values()); Collections.sort(mapValues, new Comparator<Integer>() { @Override//from w w w. j a va 2 s . co m public int compare(Integer o1, Integer o2) { return o1.compareTo(o2) * -1; } }); Collections.sort(mapKeys); LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); Iterator<Integer> valueIt = mapValues.iterator(); while (valueIt.hasNext()) { Object val = valueIt.next(); Iterator<String> keyIt = mapKeys.iterator(); while (keyIt.hasNext()) { Object key = keyIt.next(); String comp1 = passedMap.get(key).toString(); String comp2 = val.toString(); if (comp1.equals(comp2)) { passedMap.remove(key); mapKeys.remove(key); sortedMap.put((String) key, (Integer) val); break; } } } return sortedMap; } }