I wanted to order a bunch of "players" objects, each one has an attribute called points, this is the one that I wanted the sort method to work on. Here is the first part of the code:
public class Federation {
private ArrayList <Player> playerList = new ArrayList<Player>();
Collections.sort (playerList);
Collections.reverse (playerList); }
In this class I just call Collections to sort the arrayList and then to revert the order
Now the rest:
public class Player implements Comparable <Player> {
private int rankingPosition;
private Integer points;
private String name;
public int compareTo(Player arg0) {
int value = points.compareTo (arg0.points);
return (value != 0 ? value : 1);
}
}
I don't understand what
Collections.sortand
Collections.reversehave to do with compareTo. compareTo is never called, so how is used?.
What means
points.compareTo? points is just an attribute, how can it call a method?
And why is passing
arg0.points", if it should receive a player object?
I also don't fully understand
valor != 0 ? valor : 1)
It is checking if value is equal to zero, which means both objects were equal, but what it means the rest of the expression?
Thanks for the help.

New Topic/Question
Reply
MultiQuote











|