I have a Map that has strings for both keys and values.
Data is like following:
\"question1\", \"1\"
\"question9\", \"1\"
\"que
Just in case you don't wanna use a TreeMap
public static Map sortByKey(Map map) {
List> list = new ArrayList<>(map.entrySet());
list.sort(Comparator.comparingInt(Map.Entry::getKey));
Map sortedMap = new LinkedHashMap<>();
list.forEach(e -> sortedMap.put(e.getKey(), e.getValue()));
return sortedMap;
}
Also, in-case you wanted to sort your map on the basis of values
just change Map.Entry::getKey
to Map.Entry::getValue