一个学生类的实体类
@Data
public class Student {
private Long id;
private String name;
private int age;
private Double height;
public Student(Long id, String name, int age, Double height) {
this.id = id;
this.name = name;
this.age = age;
this.height = height;
}
然后我们测试下三种排序方式:
public class ListStreamSortTest {
public static void main(String[] args) {
// 一个集合中放入4个学生对象
List<Student> list = new ArrayList<>();
list.add(new Student(10002L, "ZhangSan", 19, 175.2));
list.add(new Student(10003L, "LiSi", 18, 180.1));
list.add(new Student(10004L, "Peter", 19, 170.8));
list.add(new Student(10001L, "KangKang", 18, 167.4));
// 打印默认顺序
System.out.println("默认顺序:");
list.stream().forEach(System.out::println);
// 按照id排序
System.out.println("id升序:");
list.stream().sorted(Comparator.comparing(Student::getId))
.forEach(System.out::println);
// 按照id逆序排列
System.out.println("id逆序:");
list.stream().sorted(Comparator.comparing(Student::getId).reversed())
.forEach(System.out::println);
// 按照年龄排序,再按照升高排序
System.out.println("age和height排序:");
list.stream().sorted(Comparator.comparing(Student::getAge).thenComparing(Student::getHeight))
.forEach(System.out::println);
}
}
执行结果:
默认顺序:
Student{id=10002, name='ZhangSan', age=19, height=175.2}
Student{id=10003, name='LiSi', age=18, height=180.1}
Student{id=10004, name='Peter', age=19, height=170.8}
Student{id=10001, name='KangKang', age=18, height=167.4}
id升序:
Student{id=10001, name='KangKang', age=18, height=167.4}
Student{id=10002, name='ZhangSan', age=19, height=175.2}
Student{id=10003, name='LiSi', age=18, height=180.1}
Student{id=10004, name='Peter', age=19, height=170.8}
id逆序:
Student{id=10004, name='Peter', age=19, height=170.8}
Student{id=10003, name='LiSi', age=18, height=180.1}
Student{id=10002, name='ZhangSan', age=19, height=175.2}
Student{id=10001, name='KangKang', age=18, height=167.4}
age和height排序:
Student{id=10001, name='KangKang', age=18, height=167.4}
Student{id=10003, name='LiSi', age=18, height=180.1}
Student{id=10004, name='Peter', age=19, height=170.8}
Student{id=10002, name='ZhangSan', age=19, height=175.2}
1. 刚开始用的是冒泡排序,出现数据覆盖的情况
for (int i = 0; i < list.size() - 1; i++) {
for (int j = 0; j < list.size() - 1 - i; j++) {
long time = list.get(j).getCreateAt().getTime();
long time1 = list.get(j + 1).getCreateAt().getTime();
if (time >time1) {
Object temp = list.get(j+1);
BeanUtils.copyProperties(list.get(j), list.get(j+1));
BeanUtils.copyProperties(temp, list.get(j));
}
}
2. 之后想到了Comparator比较器
public static <T> void sort(List<T> list,Comparator<? super T> )
这个里面就涉及到了Comparator 这个接口,位于java.util包下,排序是comparator能实现的功能之一,通俗地讲需要比较两个对象 谁排在前谁排在后,那么比较的方法就是:
两个对象比较的结果有三种:大于,等于,小于。
如果要按照升序排序, 则o1 小于o2,返回(负数),相等返回0,01大于02返回(正数)
如果要按照降序排序 则o1 小于o2,返回(正数),相等返回0,01大于02返回(负数)
操作如下
package com.hbsi.test;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author lbq
* @date 2020 10 15:07
*/
public class ListSort {
static class Student {
private String userName;
private String birthday;
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
List<Student> list = new ArrayList<Student>();
s1.setUserName("aa");
s1.setBirthday("1997-01-08");
s2.setUserName("bb");
s2.setBirthday("1990-11-08");
s3.setUserName("cc");
s3.setBirthday("1957-05-08");
list.add(s1);
list.add(s2);
list.add(s3);
System.out.println("排序前:");
for (Student o : list) {
System.out.println(o);
}
listSort(list);
System.out.println("排序后:");
for (Student o : list) {
System.out.println(o);
}
}
private static void listSort(List<Student> list) {
Collections.sort(list, new Comparator<Student>() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
public int compare(Student o1, Student o2) {
try {
Date dt1 = sf.parse(o1.getBirthday());
Date dt2 = sf.parse(o2.getBirthday());
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
});
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://lebron.blog.csdn.net/article/details/125123737
内容来源于网络,如有侵权,请联系作者删除!