sort the map by values

Solutions on MaxInterview for sort the map by values by the best coders in the world

showing results for - "sort the map by values"
Guillaume
29 Feb 2019
1Map -- Sort the map by values
2Write a method that can sort the Map by values
3 
4Solution:
5public static Map<String, Integer>  sortByValue(Map<String, Integer> map){
6List<Entry<String, Integer>> list = new ArrayList(map.entrySet());
7list.sort(Entry.comparingByValue());
8map = new LinkedHashMap();
9for(Entry<String, Integer> each : list) {
10map.put(each.getKey(), each.getValue());
11}
12return map;
13}