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