This post was first published on my Wordpress page link
This task could be done in different ways. Here
a discussion about some ways. In this post I will show one that I followed. Roughly speaking, the idea is convert the map
into a list with the purpose of use the classic Collection.sort() method.
The first think is that you need is to define a new class, that represents one map entry;
and implements the Comparable interface. In this class you have to implement the compareTo
method and override the toString (if you want).
Next step is to define the function that will convert any map into a list. Basically,
this function will recover the entries in the map, and transform them into custom entries
using the previous class CustomEntry.
Now, you are allowed to execute the following instructions:
and you will print the list of entries (from your original map) sorted based on its (original) values.