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,8508 gold badges26 silver badges44 bronze badges
asked Oct 7, 2017 at 13:13
1

4 Answers 4

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

answered Oct 7, 2017 at 13:36
Sign up to request clarification or add additional context in comments.

Comments

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;
 }
}
answered Oct 7, 2017 at 15:53

Comments

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))
answered Oct 7, 2017 at 21:16

Comments

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()));
answered Oct 7, 2017 at 13:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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