https://my.oschina.net/sdlvzg/blog/2243766

    基础类型List排序

    //对数字进行排序
    List nums = Arrays.asList(3,1,5,2,9,8,4,10,6,7);
    nums.sort(Comparator.reverseOrder()); //reverseOrder倒序
    System.err.println(“倒序:”+nums);

    nums.sort(Comparator.naturalOrder()); //naturalOrder自然排序即:正序
    System.err.println(“正序:”+nums);
    执行结果:

    对象List单属性排序

    List listDevs = ComparatorTest.getDevelopers();

    System.out.println(“排序前:”);
    //JAVA8的写法,循环
    listDevs.forEach((developer)->System.out.println(developer));

    //第一个写法
    Collections.sort(listDevs, new Comparator() {
    @Override
    public int compare(Developer o1, Developer o2) {
    return o1.getAge().compareTo(o2.getAge());
    }
    });
    //第二个写法,JAVA8的写法,List 接口支持直接使用 sort 该方法,不再需要使用 Collections.sort 了
    //listDevs.sort(listDevs, new Comparator() {
    // @Override
    // public int compare(Developer o1, Developer o2) {
    // return o1.getAge().compareTo(o2.getAge();
    // }
    //});

    //第三个写法,Lambda写法,JAVA8的写法
    //listDevs.sort((Developer o1, Developer o2)->o1.getAge().compareTo(o2.getAge()));

    //第四个写法,Lambda写法,JAVA8的写法
    //listDevs.sort((o1, o2)->o1.getAge().compareTo(o2.getAge()));

    //第五写法,个Lambda写法,JAVA8的写法
    //listDevs.sort(Comparator.comparing(Developer::getAge));

    //第六写法,个Lambda写法,JAVA8的写法
    Comparator ageComparator = (o1, o2)->o1.getAge().compareTo(o2.getAge());
    listDevs.sort(ageComparator); //按上面配置的顺序取值
    listDevs.sort(ageComparator.reversed()); //按上面配置的顺序反向取值

    System.out.println(“排序后:”);
    //JAVA8的写法,循环
    listDevs.forEach((developer)->System.out.println(developer));
    执行结果:

    对象List多属性排序

    // 多条件排序第一个写法,先按Age排序,再根据Salary排序
    // Comparator comparator = (o1, o2) -> {
    // if (o1.getName().equals(o2.getName())) {
    // return o1.getSalary().compareTo(o2.getSalary());
    // }
    // return o2.getAge().compareTo(o1.getAge());
    // };
    // listDevs.sort(comparator.reversed());

    // 多条件排序,先按Age排序,再根据Salary排序
    // Comparator ageComparator = (o1, o2)->o1.getAge().compareTo(o2.getAge());
    // Comparator salaryComparator = (o1, o2)->o1.getSalary().compareTo(o2.getSalary());
    // listDevs.sort(ageComparator.thenComparing(salaryComparator));

    // 多条件排序第二个写法,先按Age排序,再根据Salary排序
    listDevs.sort(Comparator.comparing(Developer::getAge).thenComparing(Developer::getSalary));
    //———————————————————————————————————————————————————
    Comparator byMoney = Comparator.comparing(CouponGrant::getMoney).reversed();
    Comparator byDateDesc = Comparator.comparing(CouponGrant::getGrantdate);
    listCanUse.sort(byMoney.thenComparing(byDateDesc));