I am trying to create a TreeMap that sorts a created map by value. My code right now is:
public static TreeMap<String, Integer> sortedMap(Map<String, Integer> map)
{
TreeMap<String, Integer> freq = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String s1, String s2)
{
return map.get(s1) >= map.get(s2) ? -1 : 1;
}
});
freq.putAll(map);
return freq;
}
However, from my understanding, this would have a time complexity of around O(n2), and wanted to see if I could use something like merge sort or just a factor way to sort the map.