I needed to sort my treemap based on it's value. The requirements of what I'm doing are such that I have to use a sorted map. I tried the solution here: Sort a Map<Key, Value> by values (Java) however as the comments say, this will make getting values from my map not work. So, instead I did the following:
class sorter implements Comparator<String> {
Map<String, Integer> _referenceMap;
public boolean sortDone = false;
public sorter(Map<String, Integer> referenceMap) {
_referenceMap = referenceMap;
}
public int compare(String a, String b) {
return sortDone ? a.compareTo(b) : _referenceMap.get(a) >= _referenceMap.get(b) ? -1 : 1;
}
}
So I leave sortDone to false until I'm finished sorting my map, and then I switch sortDone to true so that it compares things as normal. Problem is, I still cannot get items from my map. When I do myMap.get(/anything/) it is always null still.
I also do not understand what the comparator inconsistent with equals even means.