Answers for "how to sort elements in map using value"

1

javascript sort map by value

var mapAsc = new Map([...map.entries()].sort((a,b) => a[0] > b[0]));
Posted by: Guest on December-23-2020
1

Sort the map by values

Map -- Sort the map by values
Write a method that can sort the Map by values
 
Solution:
public static Map<String, Integer>  sortByValue(Map<String, Integer> map){
List<Entry<String, Integer>> list = new ArrayList(map.entrySet());
list.sort(Entry.comparingByValue());
map = new LinkedHashMap();
for(Entry<String, Integer> each : list) {
map.put(each.getKey(), each.getValue());
}
return map;
}
Posted by: Guest on September-29-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language