Reputation:
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?
Upvotes: 0
Views: 63
Reputation: 2106
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))
Upvotes: 1
Reputation: 31437
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;
}
}
Upvotes: 1
Reputation: 3037
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()));
Upvotes: 0
Reputation: 1153
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:
Upvotes: 1