You cannot sort Map values, especially not HashMap , which cannot be sorted at all.
Instead, you can sort the entries:
List<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>(map.entrySet()); Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() { public int compare( Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) { return entry1.getValue().compareTo(entry2.getValue()); } });
sorts records in ascending order of quantity.
source share