František Pártl
František Pártl

Reputation: 37

How to sort Java Map by value and if value is the then by key

I need to sort this TreeMap by values and if value is the same like 1 or 2 in this case then I need to sort these elements by their keys. How can I do it?

{65=2, 67=1, 69=4, 72=2, 73=5, 77=1, 78=2, 79=3, 81=1, 83=7, 84=6, 85=2, 87=2, 89=1}

So the result would looks like this

{83=7, 84=6, 73=5, 69=4, 79=3, 65=2, 72=2, 78=2, 85=2, 87=2, 67=1, 77=1, 81=1, 89=1}

Thank you for help.

Upvotes: 1

Views: 93

Answers (1)

dxdy
dxdy

Reputation: 540

You can't actually sort a TreeMap by its values intuitively. This post on the site explains how to tackle your predicament.

Upvotes: 1

Related Questions