一、jdk1.8之前
Collections.sort(temp, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return o2.getAge()-o1.getAge();//降序
// return o1.getAge()-o2.getAge();升序
}
});
二、jdk1.8之后
升序
//升序
temp = temp.stream().sorted(Comparator.comparing(User::getAge))
.collect(Collectors.toList());
降序
//降序
temp = temp.stream().sorted(Comparator.comparing(User::getAge)
.reversed()).collect(Collectors.toList());
多字段排序
//多字段排序
temp = temp.stream()
.sorted(
Comparator.comparing(User::getAge)
.reversed().thenComparing(Comparator.comparing(User::getGrade))