借鉴博客:https://blog.csdn.net/feinifi/article/details/120302824
使用集合类Collections.sort()方法可对list数据排序,使用案例如下:
public static void main(String[] args) { List<Person> list = new ArrayList<Person>(); list.add(new Person(1,"jack",18, false)); list.add(new Person(2,"paul",28, true)); list.add(new Person(3,"zion",20, false)); list.add(new Person(4,"jone",30, true)); list.forEach(System.out::println); //利用Collections.sort方法按照年龄排序,默认升序 // Collections.sort(list, (a, b)->{ // return b.getAge() - a.getAge();//降序 // }); // System.out.println("================================"); // list.forEach(System.out::println); //直接使用List集合sort方法按照姓名排序,默认字母升序
//我改成了b-a让t排在f前面,因为业务要求true为先
list.sort((a,b)->String.valueOf(b.isFinished()).compareTo(String.valueOf(a.isFinished()))); System.out.println("================================"); list.forEach(System.out::println); }
。