Here you can find the source of sortByComparator(HashMap
public static HashMap<String, Integer> sortByComparator(HashMap<String, Integer> unsortMap, final boolean ascendingorder)
//package com.java2s; //License from project: Open Source License import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map.Entry; public class Main { public static HashMap<String, Integer> sortByComparator(HashMap<String, Integer> unsortMap, final boolean ascendingorder) { List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet()); // Sorting the list based on values Collections.sort(list, new Comparator<Entry<String, Integer>>() { public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { if (ascendingorder) { return o1.getValue().compareTo(o2.getValue()); } else { return o2.getValue().compareTo(o1.getValue()); }//from ww w . j ava 2 s. c o m } }); // Maintaining insertion order with the help of LinkedList HashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); for (Entry<String, Integer> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } }