So let's say I have a map where the keys are names and values are ages, is there a simple way to get a list of all the names sorted by age?
For example - lets say the map contains the following pairs:
"Mark" - 20; "Steve" - 20; "Peter" - 28; "Sam" - 24; "Sally" - 25;
Is there a quick and easy way to turn this map into an arraylist of names where the names are sorted in the order of their age?
So for this example, the returned list should be ["Mark","Steve","Sam","Sally","Peter"]
As for now the best way I could think of was to create a class "Person" that has fields "Age" and "Name", create a list of Object Person, sort that list using Comparator, and then put the names from that list into a list of string containing only their names.
This however seems like a very roundabout way of achieving this, so I am wondering if there is a more efficient way of doing this, without having to rely on a new class.