I'm using the solution from this question to sort the String values in a LinkedHashMap
. However the sorting simply doesn't work. Here is the code I wrote.
Map<Integer, String> sortedMap = myMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Map.Entry<Integer, String>::getKey,
Map.Entry<Integer, String>::getValue));
myMap = new LinkedHashMap<Integer, String>(sortedMap);
The weird thing is that it is sorting the Integer
keys when both comparingByValue
and comparingByKey
methods are used. So it definitely is sorting, just not the String
values but in both cases the Integer
keys. I don't understand what I'm doing wrong here.