0

I have a Score System that I want to create, in which there is a list of players ranging their score from highest to lowest.

My PlayerObject.class Class:

public class PlayerObject {

    private String playerName;
    private int playerScore;

    public int getScore() {
        return this.playerScore;
    }

    public String getName() {
        return this.playerName;
    }

    public void setNameAndScore(String givenName, int givenScore) {
        this.playerName = givenName;
        this.playerScore = givenScore;
    }

}

My Array:

ArrayList<PlayerObject> allPlayers = new ArrayList<PlayerObject>();

Any idea how I can sort each player in the array list based on their playerScore attribute?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
  • Possible duplicate of [Sort ArrayList of custom Objects by property](https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – Zabuzard Oct 07 '17 at 21:19

4 Answers4

1

There are a lot of ways you can do it. First this is PlayerObject class:

public class PlayerObject implements Comparable<PlayerObject> {
    private String playerName;
    private int playerScore;

    public PlayerObject(String playerName, int playerScore) {
        this.playerName = playerName;
        this.playerScore = playerScore;
    }

    public String getPlayerName() {
        return playerName;
    }

    public int getPlayerScore() {
        return playerScore;
    }

    @Override
    public int compareTo(PlayerObject o) {
        return Integer.compare(playerScore, o.playerScore);
    }
}

And this is how you can sort it:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");

        List<PlayerObject> players = new ArrayList<>(2);
        players.add(new PlayerObject("player1", 2));
        players.add(new PlayerObject("player2", 4));

        // if PlayerObject implements Comparable<PlayerObject>
        Collections.sort(players);
        // or if you want explicit Comparator
        players.sort(new Comparator<PlayerObject>() {
            @Override
            public int compare(PlayerObject o1, PlayerObject o2) {
                return Integer.compare(o1.getPlayerScore(), o2.getPlayerScore());
            }
        });
        // or you can use lambda if you use Java 8
        players.sort((o1, o2) -> Integer.compare(o1.getPlayerScore(), o2.getPlayerScore()));
        // or even more concise
        players.sort(Comparator.comparingInt(PlayerObject::getPlayerScore));
    }
}

Here is a documentation that will help you:

Comparable

Comparator

TheKarlo95
  • 1,144
  • 9
  • 17
1

One of the possible way to implement Comparable in your PlayerObject class and override compareTo method.

public class PlayerObject implements Comparable<PlayerObject> {

    ...
    ...
    @Override
    public int compareTo(PlayerObject o) {
     // You can interchange the return value (-1 and 1) to change the sorting order

         if(getPlayerScore() > o.getPlayerScore())
         {
           return -1
         }
         else if(getPlayerScore() < o.getPlayerScore())
         {
           return 1;
         }
      return 0;
    }
}
Ravi
  • 30,829
  • 42
  • 119
  • 173
1

With java 8, you can do it that way, without implementing any interface :

allPlayers = allPlayers.stream()
    .sorted(Comparator.comparingInt(PlayerObject::getScore))
    .collect(Collectors.toList());

Or just :

Collections.sort(allPlayers, Comparator.comparingInt(PlayerObject::getScore))
Paul Lemarchand
  • 2,068
  • 1
  • 15
  • 27
0

Consider using comparator.

Classic one

Collections.sort(allPlayers, new Comparator<PlayerObject>() {
    @Override
    public int compare(PlayerObject p1, PlayerObject p2) {
        return p1.getScore().compareTo(p2.getScore());
    }
});

And with java-8 Lambda support

allPlayers.sort(
      (PlayerObject p1, PlayerObject p2) -> p1.getScore().compareTo(h2.getScore()));
Vino
  • 2,807
  • 1
  • 26
  • 22