Skip to content
Advertisement

How to sort the hashmap in descending order by values and if the values are the same then by key in ascending order

I have a HashMap<Integer, Integer> named “relevance” e.g {2: 3, 1: 3, 3: 3, 5: 4, 4: 4, 6: 3} and I want do DESC sorting by values.

JavaScript

I get a hashmap {5: 4, 4: 4, 2: 3, 1: 3, 3: 3, 6: 3}. How can I sort ascending keys with the same values?

Advertisement

Answer

You can “chain” Comparators by adding calls to thenComparing:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement