0

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;
        }

    }
tsolakp
  • 5,858
  • 1
  • 22
  • 28
Igor K
  • 67
  • 1
  • 5
  • 2
    `Topic extends Comparable` Are you sure you mean to define a generic type `Topic` that hides the class or interface of the same name? Everywhere inside the method, `Topic` will refer to a comparable, and not the actual interface/class/type that you think it does. – Darth Android Oct 19 '18 at 16:39
  • So then I am sorry for stupid question, but how can I fix it ? – Igor K Oct 19 '18 at 16:45
  • @Igor K. Include the class for `Topic`. – tsolakp Oct 19 '18 at 17:16
  • If `Topic` does not already implement `Comparable`, then you might have to define a new class that extends `Topic` and implements `Comparable`. – Darth Android Oct 19 '18 at 17:17

1 Answers1

0

You could declare your entriesSortedByValues method as non generic (which would hide the Topic class, as stated at first comment of your question):

static 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;
}

and then make your Topic class implement Comparable:

public class Topic implements Comparable<Topic> {
    private final int id;
    private final String topic;
    private final int score;

    public Topic(int id, String topic, int score) {
        this.id = id;
        this.topic = topic;
        this.score = score;
    }

    public int getScore() {
        return score;
    }

    @Override
    public int compareTo(Topic o) {
        return Integer.compare(getScore(), o.getScore());
    }
}
y.luis.rojo
  • 1,794
  • 4
  • 22
  • 41