Skip to content

Instantly share code, notes, and snippets.

@cankush625
Created July 7, 2021 05:32
Show Gist options
  • Save cankush625/13dbba61eaf5621bb2ad7166c623dd3b to your computer and use it in GitHub Desktop.
Save cankush625/13dbba61eaf5621bb2ad7166c623dd3b to your computer and use it in GitHub Desktop.
private HashMap<Integer, Integer> sortByValueAscending(HashMap<Integer, Integer> numCounts) {
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(numCounts.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment