为了账号安全,请及时绑定邮箱和手机立即绑定

如何按属性对对象的数组列表进行排序?

/ 猿问

如何按属性对对象的数组列表进行排序?

UYOU 2019-11-14 14:17:40

比方说你有一个Arraylist的HockeyPlayer对象。


如果它们都具有int GoalScored变量,则如何排序?您如何按GoalScored对它们进行排序?


查看完整描述

3 回答

?
汪汪一只猫

您可以使用Collections.sort自定义Comparator<HockeyPlayer>。


    class HockeyPlayer {

        public final int goalsScored;

        // ...

    };


    List<HockeyPlayer> players = // ...


    Collections.sort(players, new Comparator<HockeyPlayer>() {

        @Override public int compare(HockeyPlayer p1, HockeyPlayer p2) {

            return p1.goalsScored - p2.goalsScored; // Ascending

        }


    });

比较部分也可以这样写:


players.sort(Comparator.comparingInt(HockeyPLayer::goalsScored));

或者,您可以制作HockeyPlayer implementsComparable<HockeyPlayer>。这定义了所有HockeyPlayer对象的自然顺序。使用a Comparator更灵活,因为不同的实现可以按名称,年龄等进行排序。


也可以看看

Java:实现Comparable和之间有什么区别Comparator?

为了完整起见,我应该提醒您注意,return o1.f - o2.f由于可能存在溢出,必须非常谨慎地使用“按减法比较”快捷方式(请阅读:有效的Java 2nd Edition:项目12:考虑实现Comparable)。大概曲棍球不是一项运动,运动员可以得分而导致进球=)


查看完整回答
反对 回复 2019-11-14
?
吃鸡游戏

正如@ user6158055所建议的那样,它是一个带有的衬里Java 8,如下所示:


Collections.sort(

                hockeyPlayerList,

                (player1, player2) -> player1.getGoalsScored()

                        - player2.getGoalsScored());

完整的示例来描述相同的内容:


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;


public class Main {


    public static void main(String[] args) {

        List<HockeyPlayer> hockeyPlayerList = new ArrayList<>();

        hockeyPlayerList.add(new HockeyPlayer("A", 3));

        hockeyPlayerList.add(new HockeyPlayer("D", 10));

        hockeyPlayerList.add(new HockeyPlayer("B", 2));


        System.out.println("Before Sort based on goalsScored\n");


        hockeyPlayerList.forEach(System.out::println);


        System.out.println("\nAfter Sort based on goalsScored\n");


        Collections.sort(

                hockeyPlayerList,

                (player1, player2) -> player1.getGoalsScored()

                        - player2.getGoalsScored());


        hockeyPlayerList.forEach(System.out::println);

    }


    static class HockeyPlayer {


        private String name;

        private int goalsScored;


        public HockeyPlayer(final String name, final int goalsScored) {

            this.name = name;

            this.goalsScored = goalsScored;

        }


        public String getName() {

            return name;

        }


        public void setName(String name) {

            this.name = name;

        }


        public int getGoalsScored() {

            return goalsScored;

        }


        public void setGoalsScored(int goalsScored) {

            this.goalsScored = goalsScored;

        }


        @Override

        public String toString() {

            return "HockeyPlayer [name=" + name + ", goalsScored="

                    + goalsScored + "]";

        }


    }

}

输出:


Before Sort based on goalsScored


HockeyPlayer [name=A, goalsScored=3]

HockeyPlayer [name=D, goalsScored=10]

HockeyPlayer [name=B, goalsScored=2]


After Sort based on goalsScored


HockeyPlayer [name=B, goalsScored=2]

HockeyPlayer [name=A, goalsScored=3]

HockeyPlayer [name=D, goalsScored=10]


查看完整回答
反对 回复 2019-11-14
?
HUX布斯

Java 8只需一行:


Collections.sort(players, (p1, p2) -> p1.getGoalsScored() - p2.getGoalsScored());


查看完整回答
反对 回复 2019-11-14

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信