java对List排序
java对List排序
偷懒用个插件
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
1.对象类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
//名称
private String name;
//年龄
private Integer age;
//身高
private Double height;
}
2.测试类
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static java.util.Comparator.comparing;
class JavaTestApplicationTests {
@Test
void contextLoads() {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("A1", 18, 1.76));
studentList.add(new Student("B1", 16, 1.61));
studentList.add(new Student("C1", 19, 1.82));
studentList.add(new Student("D1", 17, 1.67));
studentList.add(new Student("E1", 18, 1.67));
// 排序前输出
studentList.forEach(System.out::println);
System.out.println("-------------------------------------------------------");
// 按年龄排序
// 降序
studentList.sort(comparing(Student::getAge).reversed());
studentList.forEach(System.out::println);
System.out.println("-------------------------------------------------------");
// 升序
studentList.sort(comparing(Student::getAge));
studentList.forEach(System.out::println);
System.out.println("-------------------------------------------------------");
// 按年龄排序,年龄相同再按身高排序
// 降序
studentList.sort(comparing(Student::getAge).thenComparing(Student::getHeight).reversed());
studentList.forEach(System.out::println);
System.out.println("-------------------------------------------------------");
// 升序
studentList.sort(comparing(Student::getAge).thenComparing(Student::getHeight));
studentList.forEach(System.out::println);
System.out.println("-------------------------------------------------------");
}
@Test
void contextLoads2() {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("A1", 18, 1.76));
studentList.add(new Student("B1", 16, 1.61));
studentList.add(new Student("C1", 19, 1.82));
studentList.add(new Student("D1", 17, 1.67));
studentList.add(new Student("E1", 18, 1.67));
List<Student> studentList2 = new ArrayList<>();
studentList2.add(new Student("A2", 8, 1.76));
studentList2.add(new Student("B2", 6, 1.61));
studentList2.add(new Student("C2", 9, 1.82));
studentList2.add(new Student("D2", 7, 1.67));
studentList2.add(new Student("E2", 8, 1.67));
List<Student> studentList3 = new ArrayList<>();
studentList3.add(new Student("A3", 28, 1.76));
studentList3.add(new Student("B3", 26, 1.61));
studentList3.add(new Student("C3", 29, 1.82));
studentList3.add(new Student("D3", 27, 1.67));
studentList3.add(new Student("E3", 28, 1.67));
List<Student> studentList4 = new ArrayList<>();
studentList4.add(new Student("A4", 38, 1.76));
studentList4.add(new Student("B4", 36, 1.61));
studentList4.add(new Student("C4", 39, 1.82));
studentList4.add(new Student("D4", 37, 1.67));
studentList4.add(new Student("E4", 38, 1.67));
List<Student> studentList5 = new ArrayList<>();
studentList5.add(new Student("A5", 58, 1.76));
studentList5.add(new Student("B5", 56, 1.61));
studentList5.add(new Student("C5", 59, 1.82));
studentList5.add(new Student("D5", 57, 1.67));
studentList5.add(new Student("E5", 58, 1.67));
List<Student> studentList6 = new ArrayList<>();
studentList6.add(new Student("A6", 48, 1.76));
studentList6.add(new Student("B6", 46, 1.61));
studentList6.add(new Student("C6", 49, 1.82));
studentList6.add(new Student("D6", 47, 1.67));
studentList6.add(new Student("E6", 48, 1.67));
List<List<Student>> lists = new ArrayList<>();
lists.add(studentList);
lists.add(studentList2);
lists.add(studentList3);
lists.add(studentList4);
lists.add(studentList5);
lists.add(studentList6);
//排序前
System.out.println("排序前");
for (List<Student> list : lists) {
System.out.println(list);
}
for (List<Student> list : lists) {
//降序(大到小)
list.sort(comparing(Student::getAge).reversed());
}
lists = lists.stream().sorted((o1, o2) -> {
for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) {
//大到小
int c = o2.get(i).getAge().compareTo(o1.get(i).getAge());
if (c != 0) {
return c;
}
}
return Integer.compare(o1.size(), o2.size());
}).collect(Collectors.toList());
//排序后
System.out.println("排序后");
for (List<Student> list : lists) {
System.out.println(list);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗