Sobes.tech
Junior — Senior

Sorting Map<String, Integer> by its values

livecode

Task condition

There is an unsorted dictionary of type Map<String, Integer>. It is required to rearrange its elements so that they are in ascending order of values, and return the resulting sorted dictionary.

Expected output:

One=1
Two=2
Three=3
Four=4
Five=5
public class MapComparator {
    public static void main(String[] args) {
        Map<String, Integer> nonSortedMap = Map.of(
            "Two", 2,
            "Three", 3,
            "Five", 5,
            "One", 1,
            "Four", 4
        );

        Map<String, Integer> sortedMap = sortByValue(nonSortedMap);

        sortedMap.entrySet().forEach(System.out::println);
    }

    static Map<String, Integer> sortByValue(Map<String, Integer> map) {
        return map; // Implement sorting by values
    }
}
"""}]}]}