【Java】List排序
package com.crawler.boos.chrome.test; import com.alibaba.fastjson.JSONArray; import lombok.Data; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.Test; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @Author: * @Date: 2022/7/7 20:30 * @Description: 测试样例 * @Version: v1.0 */ @SpringBootTest public class TestDemo extends AbstractTestNGSpringContextTests { /** * list排序(基本数据类型list) * 如:List<Integer> 类型的 */ @Test public void testListSort1() { List<Integer> integers = Arrays.asList(6, 3, 9, 1, 2); System.out.println(String.format("排序前:%s", integers)); // [6, 3, 9, 1, 2] // 1、正序排序(从小到大) integers.sort(null); // 默认正序排列(从小到大) System.out.println(String.format("排序后 默认正序排列(从小到大):%s", integers)); // [1, 2, 3, 6, 9] // 2、倒叙排序(从大到小) integers.sort((o1, o2) -> o2 - o1); // 这是lambda表达式简写的 System.out.println(String.format("排序后 倒序排列(从大到小)简写方式:%s", integers)); // [9, 6, 3, 2, 1] } @Data class Student { private Integer age; private String name; private String birthday; Student(Integer age, String name, String birthday) { this.age = age; this.name = name; this.birthday = birthday; } } /** * list排序(对象类型list) * 如:ArrayList<Student> 类型的 * * @throws ParseException */ @Test public void testListSort2() throws ParseException { Student student1 = new Student(26, "26岁的人", "1995-01-24"); Student student2 = new Student(38, "38岁的人", "1985-05-24"); Student student3 = new Student(24, "24岁的人", "1996-03-20"); Student student4 = new Student(20, "20岁的人", "2001-03-24"); ArrayList<Student> students = new ArrayList<>(); students.add(student1); students.add(student2); students.add(student3); students.add(student4); System.out.println(JSONArray.toJSON(students)); // [{"birthday":"1995-01-24","name":"26岁的人","age":26},{"birthday":"1985-05-24","name":"38岁的人","age":38},{"birthday":"1996-03-20","name":"24岁的人","age":24},{"birthday":"2001-03-24","name":"20岁的人","age":20}] // 1、根据Student对象中的age属性 正序排序(从小到大) students.sort((stu1, stu2) -> stu1.age - stu2.age); System.out.println(JSONArray.toJSON(students)); // [{"birthday":"2001-03-24","name":"20岁的人","age":20},{"birthday":"1996-03-20","name":"24岁的人","age":24},{"birthday":"1995-01-24","name":"26岁的人","age":26},{"birthday":"1985-05-24","name":"38岁的人","age":38}] // 2、根据Student对象中的age属性 倒叙排序(从大到小) students.sort((stu1, stu2) -> stu2.age - stu1.age); System.out.println(JSONArray.toJSON(students)); // [{"birthday":"1985-05-24","name":"38岁的人","age":38},{"birthday":"1995-01-24","name":"26岁的人","age":26},{"birthday":"1996-03-20","name":"24岁的人","age":24},{"birthday":"2001-03-24","name":"20岁的人","age":20}] } }
如果忍耐算是坚强 我选择抵抗 如果妥协算是努力 我选择争取
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了