List<Car> catList = new ArrayList<>();
//加入4个对象
Car car1 = new Car();
car1.setTestFiled(1.02);
Car car2 = new Car();
car2.setTestFiled(null);
Car car3 = new Car();
car3.setTestFiled(1.04);
Car car4 = new Car();
car4.setTestFiled(1.05);
catList.add(car1);
catList.add(car2);
catList.add(car3);
catList.add(car4);
catList.sort(Comparator.comparing(Car::getTestFiled,Comparator.nullsLast(Double::compareTo)));
System.out.println("正序:");
System.out.println(catList);
catList.sort(Comparator.comparing(Car::getTestFiled,Comparator.nullsFirst(Double::compareTo)).reversed());
System.out.println("倒序:");
Java-有空值(NULL) List 对象按字段排序(空值在最后)
最新推荐文章于 2024-08-07 14:09:59 发布