I'm working on a Java project that asks me to implement the function obtainRanking() : void with the following description:
Sort the players list by using the class method sort (List) from the Collections class, a method to sort the objects of a collection. For that, the object's class (Player in our case), should implement the interface "Comparable" and its method compareTo.
So far, this is how I implemented the interface Comparable:
package modeloqytetet;
public interface Comparable {
public int compareTo(Object otroJugador);
}
Inside class Player this is how I implemented the said method:
@Override
public int compareTo(Object otherJugador) {
int otherCapital = ((Player) otherJugador).getCapital();
return otherCapital-getCapital();
}
Now, the method obtainRanking() : void should be implemented in other class and I don't know how to do it. I've been trying to figure out by looking some examples around the internet but nothing seems to work.
Any help would be appreciated.
obtainRankingshould not return void. Nevertheless, You can pass the the Comparator tosortmethod that you use in the method in other class.ComparableinterfaceobtainRankingmethod then, can't you?