开源项目SDK:https://github.com/mingyang66/spring-parent
个人文档:https://mingyang66.github.io/raccoon-docs/#/
一、首先定义一个三个属性的People类
public class People {
private String name;
private int age;
private int height;
public People(String name, int age, int height) {
this.name = name;
this.age = age;
this.height = height;
}
}
二、定义一个List集合列表
List<People> list = List.of(
new People("Emily", 18, 20),
new People("田晓霞", 28, 165),
new People("lili", 19, 28),
new People("lili", 19, 21),
new People("Tom", 20, 22)
);
三、列表先按照age升序再按照height升序排序
List<People> data = list.stream().sorted(Comparator.comparing(People::getAge).thenComparing(People::getHeight)).toList();
sorted方法默认是升序
四、列表先按照age降序再按照height降序排列
List<People> data = list.stream().sorted(Comparator.comparing(People::getAge).thenComparing(People::getHeight).reversed()).toList();
标签:Java,People,age,List,height,new,排序,降序 From: https://blog.csdn.net/yaomingyang/article/details/139606495排序后添加reversed方法自动反转排序结果,即按先按照age降序,再按照height降序排列。