@Data
public class StudentInfo implements Comparable<StudentInfo> {
//名称
private String name;
//性别 true男 false女
private Boolean gender;
//年龄
private Integer age;
//身高
private Double height;
//出生日期
private LocalDate birthday;
public StudentInfo(String name, Boolean gender, Integer age, Double height, LocalDate birthday) {
this.name = name;
this.gender = gender;
this.age = age;
this.height = height;
this.birthday = birthday;
}
@Override
public String toString() {
return "StudentInfo{" +
"name='" + name + '\'' +
", gender=" + gender +
", age=" + age +
", height=" + height +
", birthday=" + birthday +
'}';
}
public static void printStudents(List<StudentInfo> studentInfos) {
System.out.println("[姓名]\t\t[性别]\t\t[年龄]\t\t[身高]\t\t[生日]");
System.out.println("----------------------------------------------------------");
studentInfos.forEach(s -> System.out.println(s.toString()));
System.out.println(" ");
}
@Override
public int compareTo(StudentInfo o) {
return this.age.compareTo(o.getAge());
}
public String getName() {
return name;
}
public Boolean getGender() {
return gender;
}
public Integer getAge() {
return age;
}
public Double getHeight() {
return height;
}
=================================================================================================================
public class sort {
public static void main(String[] args) {
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("李小明", true, 18, 1.76, LocalDate.of(2001, 3, 23)));
studentList.add(new StudentInfo("张小丽", false, 18, 1.61, LocalDate.of(2001, 6, 3)));
studentList.add(new StudentInfo("王大朋", true, 19, 1.82, LocalDate.of(2000, 3, 11)));
studentList.add(new StudentInfo("陈小跑", false, 17, 1.67, LocalDate.of(2002, 10, 18)));
StudentInfo.printStudents(studentList);
//final List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());
final List<StudentInfo> studentsSortName = studentList.stream()
.sorted(Comparator.comparing(StudentInfo::getAge).reversed()
.thenComparing(StudentInfo::getHeight))
.collect(Collectors.toList());
StudentInfo.printStudents(studentsSortName);
}
}
运行结果
[姓名] [性别] [年龄] [身高] [生日]
----------------------------------------------------------
StudentInfo{name='李小明', gender=true, age=18, height=1.76, birthday=2001-03-23}
StudentInfo{name='张小丽', gender=false, age=18, height=1.61, birthday=2001-06-03}
StudentInfo{name='王大朋', gender=true, age=19, height=1.82, birthday=2000-03-11}
StudentInfo{name='陈小跑', gender=false, age=17, height=1.67, birthday=2002-10-18}
[姓名] [性别] [年龄] [身高] [生日]
----------------------------------------------------------
StudentInfo{name='王大朋', gender=true, age=19, height=1.82, birthday=2000-03-11}
StudentInfo{name='张小丽', gender=false, age=18, height=1.61, birthday=2001-06-03}
StudentInfo{name='李小明', gender=true, age=18, height=1.76, birthday=2001-03-23}
StudentInfo{name='陈小跑', gender=false, age=17, height=1.67, birthday=2002-10-18}
如果年龄相同按照身高大的排在前面thenComparing默认从小到大排
public static void main(String[] args) {
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("李小明", true, 18, 1.76, LocalDate.of(2001, 3, 23)));
studentList.add(new StudentInfo("张小丽", false, 18, 1.61, LocalDate.of(2001, 6, 3)));
studentList.add(new StudentInfo("王大朋", true, 19, 1.82, LocalDate.of(2000, 3, 11)));
studentList.add(new StudentInfo("陈小跑", false, 17, 1.67, LocalDate.of(2002, 10, 18)));
StudentInfo.printStudents(studentList);
Function<StudentInfo, Integer> function = p -> -p.getHeight().compareTo(p.getHeight());
//final List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());
final List<StudentInfo> studentsSortName = studentList.stream()
.sorted(Comparator.comparing(StudentInfo::getAge).reversed()
.thenComparing((p,p1)-> p1.getHeight().compareTo(p.getHeight())))
.collect(Collectors.toList());
StudentInfo.printStudents(studentsSortName);
}
运行结果
[姓名] [性别] [年龄] [身高] [生日]
----------------------------------------------------------
StudentInfo{name='李小明', gender=true, age=18, height=1.76, birthday=2001-03-23}
StudentInfo{name='张小丽', gender=false, age=18, height=1.61, birthday=2001-06-03}
StudentInfo{name='王大朋', gender=true, age=19, height=1.82, birthday=2000-03-11}
StudentInfo{name='陈小跑', gender=false, age=17, height=1.67, birthday=2002-10-18}
[姓名] [性别] [年龄] [身高] [生日]
----------------------------------------------------------
StudentInfo{name='王大朋', gender=true, age=19, height=1.82, birthday=2000-03-11}
StudentInfo{name='李小明', gender=true, age=18, height=1.76, birthday=2001-03-23}
StudentInfo{name='张小丽', gender=false, age=18, height=1.61, birthday=2001-06-03}
StudentInfo{name='陈小跑', gender=false, age=17, height=1.67, birthday=2002-10-18}