I have a map: TreeMap<String, Integer> m = new TreeMap<>();
where I have a whole alphabet and values, which shows how many times each letter was found in my text.
I want to sort that map in descending count order; that is, the most frequent letter is on the first line, and the last line of output indicates the least frequent letter. If two letters have the same frequency, then the letter which comes first in the alphabet must appear first. How to make it?
I tried with Comparator:
public int compare(String a, String b) {
if (base.get(a) >= base.get(b) && a.compareToIgnoreCase(b) < 0) {
return -1;
} else {
return 1;
}
}
but still, its not it, the output is:
D 3
E 3
A 2
S 5
Guys ... Found this before, this didnt help at all. Good output should be:
S 5
D 3
E 3
A 2