0

I have a list that contains names of users.

List<String> members = new ArrayList<>();

And I store the users and their points like this:

Map<String, Integer> membersAndPoints = new HashMap<>();

for(String member : members) {
    membersAndPoints.put(member, Utils.getPoints(member));
}

Note that the points are not unique, if more than one user has the same points, they have to be in a similar position. Then I want to sort the members depending on the values of the map, that's what I dont know how to do. If a member has 1000 points, and no one else has more than that, that member would be in the position 0 of the ranking, and so on.

List<String> ranking = new ArrayList<>(); 

Looking at the respones I got I did this, but I'm not sure if its that what I want or not.

Collections.sort(clans, new Comparator<String>() {
                    @Override
                    public int compare(String o1, String o2) {
                        return Integer.valueOf(Utils.getPoints(o1)).compareTo(Integer.valueOf(Utils.getPoints(o2)));
                    }
                });

Edit: I did this, and it works:

List<String> list = new ArrayList<String>() {
            {
                add("Example1");
                add("Example2");
                add("Example3");
                add("Example4");
                add("Example5");
            }
        };
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return getFor(s2).compareTo(getFor(s1));
            }
        });
        for(String s : list) {
            System.out.println(String.format("%s : %s", s, getFor(s)));
        }
    }

    public static Integer getFor(String s) {
        switch (s) {
        case "Example1":
            return 29;
        case "Example2":
            return 94;
        case "Example3":
            return 67;
        case "Example4":
            return 34;
        case "Example5":
            return 94;
        default:
            return 0;
        }
    }
jamezrin
  • 122
  • 9

1 Answers1

1

Use comparators. Where you should override compareTo method. There you should compare each entry set based on their value.

or

Alternatively you can use Sorted Maps also with your custom comparator. It will keep your entry set sorted as per comparators

SacJn
  • 777
  • 1
  • 6
  • 16
  • Correct but a bit short – Dici Aug 31 '15 at 09:51
  • This should rather be a comment, not an answer. – Konstantin Yovkov Aug 31 '15 at 09:51
  • @Dici yes, actually, I was editing at the same time. I could also provide code snippets but I also wanted the person to know about comparator and compareTo method by own. – SacJn Aug 31 '15 at 09:55
  • @SacJn `LinkedHashMap` is not *sorted*, it is *ordered*. You probably meant `TreeMap` – Dici Aug 31 '15 at 09:56
  • @Dici based on the comparator I provide to my map reference at the time of creating it, it sorts each new entry to it – SacJn Aug 31 '15 at 09:57
  • @SacJn No, look at the doc http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html – Dici Aug 31 '15 at 09:58
  • @Dici sorry, Just mixed up with sortedMaps with LinkedHashMap, though they actually maintain insertion order only while iterating over them . – SacJn Aug 31 '15 at 10:05
  • @SacJn yes that's what I said. It is ordered, not sorted – Dici Aug 31 '15 at 10:26