sort
是java.util.List
接口的默认方法。
List
的排序方法在Java 8
中被引入。
排序方法接受比较器作为参数,并根据指定的比较器对这个列表进行排序。
java
default void sort(Comparator<? super E> c)
示例代码:
java
import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
public class ListSort {
private static Student zs = new Student("张三", 38, "156****6549");
private static Student ls = new Student("李四", 28, "138****4267");
private static Student ww = new Student("王五", 20, "186****7391");
private static Student zs2 = new Student("张三", 26, "172****6549");
private static Student ww2 = new Student("王五", null, "196****7391");
public static void main(String[] args) {
List<Student> studentList = Arrays.asList(zs, ls, ww, zs2, ww2);
Collator collator = Collator.getInstance(Locale.CHINA);
// 按照姓名排序
Comparator<Student> nameComparator = Comparator.comparing(Student::getName, collator);
// 正序
studentList.sort(nameComparator);
// 倒序
studentList.sort(nameComparator.reversed());
System.out.println("************姓名****************");
studentList.forEach(System.out::println);
// 年龄排序(空值最前)
studentList.sort(Comparator.comparing(Student::getAge, Comparator.nullsFirst(Integer::compareTo)));
System.out.println("************年龄****************");
studentList.forEach(System.out::println);
// 手机号前三位排序
studentList.sort(Comparator.comparing(student -> student.getPhoneNum().substring(0, 3)));
System.out.println("************手机号****************");
studentList.forEach(System.out::println);
// 多字段排序(按姓名,再按年龄)
studentList.sort(Comparator.comparing(Student::getName, collator).
thenComparing(Student::getAge, Comparator.nullsLast(Comparator.naturalOrder())));
System.out.println("************多字段****************");
studentList.forEach(System.out::println);
}
public static class Student {
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 手机号
*/
private String phoneNum;
public Student(String name, Integer age, String phoneNum) {
this.name = name;
this.age = age;
this.phoneNum = phoneNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", phoneNum='" + phoneNum + '\'' +
'}';
}
}
}
排序结果:
bash
************姓名****************
Student{name='张三', age=38, phoneNum='156****6549'}
Student{name='张三', age=26, phoneNum='172****6549'}
Student{name='王五', age=20, phoneNum='186****7391'}
Student{name='王五', age=null, phoneNum='196****7391'}
Student{name='李四', age=28, phoneNum='138****4267'}
************年龄****************
Student{name='王五', age=null, phoneNum='196****7391'}
Student{name='王五', age=20, phoneNum='186****7391'}
Student{name='张三', age=26, phoneNum='172****6549'}
Student{name='李四', age=28, phoneNum='138****4267'}
Student{name='张三', age=38, phoneNum='156****6549'}
************手机号****************
Student{name='李四', age=28, phoneNum='138****4267'}
Student{name='张三', age=38, phoneNum='156****6549'}
Student{name='张三', age=26, phoneNum='172****6549'}
Student{name='王五', age=20, phoneNum='186****7391'}
Student{name='王五', age=null, phoneNum='196****7391'}
************多字段****************
Student{name='李四', age=28, phoneNum='138****4267'}
Student{name='王五', age=20, phoneNum='186****7391'}
Student{name='王五', age=null, phoneNum='196****7391'}
Student{name='张三', age=26, phoneNum='172****6549'}
Student{name='张三', age=38, phoneNum='156****6549'}