通过ArrayList对象中的特定值对ArrayList进行排序?

时间:2017-10-07 13:13:27

标签: java sorting arraylist

我有一个我想要创建的分数系统,其中有一个玩家列表,其分数从最高到最低。

我的PlayerObject.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;
    }

}

我的数组:

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

知道我如何根据 playerScore 属性对数组列表中的每个玩家进行排序?

谢谢!

4 个答案:

答案 0 :(得分:2)

有很多方法可以做到。首先这是PlayerObject类:

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);
    }
}

这就是你可以对它进行排序的方式:

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));
    }
}

这是一份可以帮助您的文档:

Comparable

Comparator

答案 1 :(得分:1)

PlayerObject课程中实施compareTo并覆盖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; } } 方法的可行方法之一。

This site can not be reached
127.0.0.1 refused to connect

答案 2 :(得分:1)

使用java 8,您可以这样做,而无需实现任何接口:

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

或者只是:

Collections.sort(allPlayers, Comparator.comparingInt(PlayerObject::getScore))

答案 3 :(得分:0)

考虑使用比较器。

经典之作

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

使用java-8 Lambda支持

allPlayers.sort(
      (PlayerObject p1, PlayerObject p2) -> p1.getScore().compareTo(h2.getScore()));