2

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.

3
  • "I don't really understand how desc can be an attribute of the checker class." Well, it isn't if you don't add it. But you don't need to add anything if you always want to sort in a certain order (whether ascending or descending) - just make the logic return the right value for that fixed order. Commented Mar 15, 2016 at 14:37
  • I named the comparator desc and then removed it since I couldn't make much sense of it. Commented Mar 15, 2016 at 14:38
  • 1
    dont pass the class PLayer, pass instead the list containing the players Commented Mar 15, 2016 at 14:40

1 Answer 1

2

If you are allowed to use Comparator in your solution then it's actually quite a bit simpler than you think.

Comparator<Player> playerSorted = Comparator
    .comparingInt(Player::getScore)
    .thenComparing(Player::getName)
    .reversed();

If you need to wrap it in another class then you can declare this Comparator as a private static final and delegate the compare method to it.

class Checker implements Comparator<Player> {
    private static final Comparator<Player> SORT_ASC = Comparator
        .comparingInt(Player::getScore).thenComparing(Player::getName);
    private static final Comparator<Player> SORT_DESC = SORT_ASC.reversed();

    private final boolean descending;

    public int compare(Player player1, Player player2) {
        Comparator<Player> sorter = descending ? SORT_DESC : SORT_ASC;
        return sorter.compare(player1, player2);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is elegant, but I'm limited by how I can implement it in my code. I need to implement a comparator class.
@Zeus I've added a comment on how to use this in your own class.
I'm getting compilation error with the Player::score syntax. Not sure what it means. error: ')' expected .comparingInt(Player::score).thenComparing(Player::name); ^ Solution.java:6: error: illegal start of type .comparingInt(Player::score).thenComparing(Player::name); ^ Solution.java:6: error: ';' expected .comparingInt(Player::score).thenComparing(Player::name);
@Zeus the answer uses capabilities introduced in Java 8. It looks like you are using an earlier version of Java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.