标签:reverse add 参考 tor array 创建 tps span art
1、使用java.util.Comparator 创建一个比较器来进行排序
public static void main(String[] args) { Student student1 = new Student (1L ,"by"); Student student2 = new Student (2L ,"zs"); Student student3 = new Student (3L ,"ls"); List<Student> students = new ArrayList<>(); students.add(student2); students.add(student3); students.add(student1); //1、原始数据输出 // [Student(id=2, name=zs), Student(id=3, name=ls), Student(id=1, name=by)] System.out.println(students); //2、使用 Comparator.comparing 进行排序(根据id顺序进行排序) // 法一 // [Student(id=1, name=by), Student(id=2, name=zs), Student(id=3, name=ls)] students.sort(Comparator.comparing(e -> e.getId())); System.out.println(students); // 法二 // [Student(id=1, name=by), Student(id=2, name=zs), Student(id=3, name=ls)] students.sort(Comparator.comparing(Student::getId)); System.out.println(students); //3、根据id倒序排序 students.sort(Comparator.comparing(Student::getId).reversed()); // [Student(id=3, name=ls), Student(id=2, name=zs), Student(id=1, name=by)] System.out.println(students); }
参考:https://blog.csdn.net/rungong123/article/details/88421272
标签:reverse add 参考 tor array 创建 tps span art
原文地址:https://www.cnblogs.com/aibaiyang/p/11395833.html