Java List 排序

1. 对于普通类型(Stirng、Integer)升序:Collections.sort(arrayList),降序:Collections.reverse(arrayList)

    public void testSortData(){
        ArrayList<String> arrayList = new ArrayList<String>(5);
        arrayList.add("AAAA");
        arrayList.add("CCCC");
        arrayList.add("BBBB");
        arrayList.add("EEEE");
        arrayList.add("DDDD");

        Collections.sort(arrayList); //升序
        //Collections.reverse(arrayList); //降序
        for (String s : arrayList) {
            System.out.println(s);
        }
    }

2. 对于复杂类型(实体类)

(1)Collections.sort 升序:  o1.getEmail().compareTo(o2.getEmail()),降序:o2.getEmail().compareTo(o1.getEmail())

    public void testSortObject() {
        List<Customer> list = new ArrayList<Customer>();
        list.add(new Customer(5, "张三", "WN001", 88));
        list.add(new Customer(8, "李四", "N002", 75));
        list.add(new Customer(9, "王五", "KN003", 99));
        list.add(new Customer(10, "赵六", "UN004", 58));
        list.add(new Customer(11, "田七", "LN005", 67));
        list.add(new Customer(12, "小八", "ON006", 58));
        list.add(new Customer(6, "唐七", "TN007", 35));
        list.add(new Customer(7, "商十", "SN008", 78));
        //使用Collections.sort排序
        Collections.sort(list, new Comparator<Customer>() {
            @Override
            public int compare(Customer o1, Customer o2) {
                return o1.getEmail().compareTo(o2.getEmail()); //升序
                //return o2.getEmail().compareTo(o1.getEmail()); //降序
            }
        });
        for (Customer s : list) {
            System.out.println(s.getEmail());
        }
    }

中文排序

    public void testSortObject() {
        List<Customer> list = new ArrayList<Customer>();
        list.add(new Customer(5, "张三", "WN001", 88));
        list.add(new Customer(8, "李四", "N002", 75));
        list.add(new Customer(9, "王五", "KN003", 99));
        list.add(new Customer(10, "赵六", "UN004", 58));
        list.add(new Customer(11, "田七", "LN005", 67));
        list.add(new Customer(12, "小八", "ON006", 58));
        list.add(new Customer(6, "唐七", "TN007", 35));
        list.add(new Customer(7, "商十", "SN008", 78));

        Collections.sort(list, new Comparator<Customer>() {
            @Override
            public int compare(Customer o1, Customer o2) {
                Collator cmp = Collator.getInstance(java.util.Locale.CHINA);// 中文首字母排序规则
                //if (cmp.compare(o1.getLastName(), o2.getLastName())>0){    //升序
                if (cmp.compare(o2.getLastName(), o1.getLastName()) > 0) {    //降序
                    return 1;
                } else {
                    return -1;
                }
            }
        });
        for (Customer s : list) {
            System.out.println(s.getLastName());
        }
    }

(2)Stream().sort 升序:Comparator.comparing(Customer::getEmail),   降序:(Customer::getEmail).reversed()

   public void testStreamSortObject() {
        List<Customer> list = new ArrayList<Customer>();
        list.add(new Customer(5, "张三", "WN001", 88));
        list.add(new Customer(8, "李四", "N002", 75));
        list.add(new Customer(9, "王五", "KN003", 99));
        list.add(new Customer(10, "赵六", "UN004", 58));
        list.add(new Customer(11, "田七", "LN005", 67));
        list.add(new Customer(12, "小八", "ON006", 58));
        list.add(new Customer(6, "唐七", "TN007", 35));
        list.add(new Customer(7, "商十", "SN008", 78));

        //List<Customer> customerList = list.stream().sorted(Comparator.comparing(Customer::getEmail)).collect(Collectors.toList());//升序
        List<Customer> customerList = list.stream().sorted(Comparator.comparing(Customer::getEmail).reversed()).collect(Collectors.toList());//降序
        for (Customer s : customerList) {
            System.out.println(s.getEmail());
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值