I'm fairly new to java thus the question. My task is to create a class Checker which uses a comparator desc to sort the players. The sorting logic is to sort the players in decreasing order by score and then if two players have the same score, the one whose name is lexicographically larger should appear first.
This is the Player class
class Player
{
String name;
int score;
}
The Comparator gets called this way
Checker check=new Checker();
.................
Arrays.sort(Player,check.desc);
This is what I tried,
class Checker implements Comparator<Player>{
public int compare(Player p1, Player p2){
if(p1.score < p2.score) return 1;
else if(p1.score > p2.score) return -1;
else if(p1.score == p2.score){
if(p1.name.compareTo(p2.name) < 0) return 1;
else if(p1.name.compareTo(p2.name) > 0) return -1;
else if (p1.name.compareTo(p2.name) == 0) return 0;
}
}
}
Could someone help me get it right. I don't really understand how desc can be an attribute of the checker class.