I should to write a comparator that will let me sort a TreeMap by getScore in instance which is Value instead of the default natural ordering. Earlier I have found one decision of my problem (TreeMap sort by value) but the problem has stayed. When I call e1.getValue they won't resolve methods of instance. How I can get them?
public class Trending {
Map<String, Topic> treeMap = new TreeMap<>();
void initialScore(int id, String topic, int score){
Topic object = new Topic(id, topic, score);
treeMap.put(topic, object);
}
static <String, Topic extends Comparable<Topic>>
SortedSet<Map.Entry<String,Topic>> entriesSortedByValues(Map<String,Topic> map) {
SortedSet<Map.Entry<String,Topic>> sortedEntries = new TreeSet<Map.Entry<String,Topic>>(
new Comparator<Map.Entry<String,Topic>>() {
@Override public int compare(Map.Entry<String,Topic> e1, Map.Entry<String,Topic> e2) {
int res = e1.getValue().compareTo(e2.getValue());
return res != 0 ? res : 1;
}
}
);
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}
}