Java 列表ArrayList排序

对任意类型集合对象进行整体排序,排序时将此接口的实现传递给Collections.sort方法或者Arrays.sort方法排序. 
实现int compare(T o1, T o2);方法,返回正数,零,负数各代表大于,等于,小于。

单一条件排序:

举例:

        List<Student> stus = new ArrayList<>();
        Student stu1 = new Student();
        Student stu2 = new Student();
        Student stu3 = new Student();
        stu1.setName("张三");
        stu1.setAge(30);

        stu2.setName("李四");
        stu2.setAge(20);

        stu3.setName("王五");
        stu3.setAge(60);

        stus.add(stu1);
        stus.add(stu2);
        stus.add(stu3);

        //对users按年龄进行排序
        Collections.sort(stus, new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                // 升序
                //return o1.getAge()-o2.getAge();
                return o1.getAge().compareTo(o2.getAge());
                // 降序
                // return o2.getAge()-o1.getAge();
                // return o2.getAge().compareTo(o1.getAge());
            }
        });
        // 输出结果
        ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

多条件排序:

举例:

        List<Student> stus = new ArrayList<>();
        Student stu1 = new Student();
        Student stu2 = new Student();
        Student stu3 = new Student();
        Student stu4 = new Student();
        Student stu5 = new Student();
        Student stu6 = new Student();

        stu1.setAge(30);
        stu2.setAge(20);
        stu3.setAge(40);
        stu4.setAge(30);
        stu5.setAge(40);
        stu6.setAge(20);

        stu1.setNum(1);
        stu2.setNum(2);
        stu3.setNum(3);
        stu4.setNum(4);
        stu5.setNum(5);
        stu6.setNum(6);

        stu1.setName("张三");
        stu2.setName("李四");
        stu3.setName("王五");
        stu4.setName("赵六");
        stu5.setName("陈七");
        stu6.setName("周八");

        stus.add(stu1);
        stus.add(stu2);
        stus.add(stu3);
        stus.add(stu4);
        stus.add(stu5);
        stus.add(stu6);

        Collections.sort(stus,new Comparator<Student>() {

            @Override
            public int compare(Student s1, Student s2) {
                int flag;
                // 首选按年龄升序排序
                flag = s1.getAge()-s2.getAge();
                if(flag==0){
                    // 再按学号升序排序
                    flag = s1.getNum()-s2.getNum();
                }
                return flag;
            }
        });

        System.out.println("年龄       学号       姓名  ");
        for(Student s : stus){
            System.out.println(s.getAge()+"   "+s.getNum()+"   "+s.getName());
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

输出结果:
这里写图片描述

自定义条件排序

        String[] order = {"语文","数学","英语","物理","化学","生物","政治","历史","地理","总分"};
        final List<String> defiderOrder = Arrays.asList(order);
        List<String> list = new ArrayList<>();
        list.add("总分");
        list.add("英语");
        list.add("政治");
        list.add("总分");
        list.add("数学");
        Collections.sort(list,new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                int io1 = defiderOrder.indexOf(o1);
                int io2 = defiderOrder.indexOf(o2);
                return io1-io2;
            }
        });

        for(String s:list){
            System.out.print(s+"   ");
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

输出结果:
这里写图片描述

CSDN 原文:https://blog.csdn.net/lx_nhs/article/details/78871295?

  • 29
    点赞
  • 137
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值