Java Map sort by value
问题 I was looking for ways of sorting Map<String, Integer> by values. I found this post, which solved my sorting problem, but not exactly. According to the post, I wrote the following code: import java.util.*; public class Sort { static class ValueComparator implements Comparator<String> { Map<String, Integer> base; ValueComparator(Map<String, Integer> base) { this.base = base; } @Override public int compare(String a, String b) { if (base.get(a) >= base.get(b)) { return 1; } else { return -1; } }