Skip to content

Instantly share code, notes, and snippets.

@cankush625
Last active July 7, 2021 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cankush625/5fa4db0382468aa07745949116f4d20e to your computer and use it in GitHub Desktop.
Save cankush625/5fa4db0382468aa07745949116f4d20e to your computer and use it in GitHub Desktop.
private HashMap<Integer, Integer> sortByValueDescending(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 (o2.getValue()).compareTo(o1.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