I am trying to sort a map to show in a dropdown. But I am not able to get any sorting done. This will return a new map. But not with the map sorted by the key as I would expect.
private Map<String, String> mapInstrumentIDs = new TreeMap<>();
Map<Object, Object> val = mapInstrumentIDs
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
I of course didn't think about that the key is actually an integer. This means sorting it as a string does not give me the expected result (as integer sort). Changing the key to Integer and converting the value will yield the expected result.
mapInstrumentIDs. It's aTreeMap, so it implicitly sorts its entries by key.