一、嵌套对象倒序的正确书写方式

描述:对象A内部封装对象B,根据B的字段做倒排

Comparator ageDescCom = Comparator.comparing(temp -> temp.getKid().getAge()).reversed();//报错:temp为objectComparator ageDescCom = Comparator.comparing((User temp) -> temp.getKid().getAge()).reversed();//有效Comparator ageDescCom = Comparator.comparing(temp -> temp.getKid().getAge(), Comparator.reverseOrder());//有效

测试代码:

@Data@Builder@AllArgsConstructor@NoArgsConstructorpublic class User extends BaseEntity {String nickname;String avatar;Integer gender;Integer age;User kid;@Overridepublic User instance() {return new User();}}public class ComparatorTest {@Testpublic void test() {User kid = User.builder().age(1).build();User kid1 = User.builder().age(2).build();User kid2 = User.builder().age(3).build();User kid3 = User.builder().age(4).build();User kid4 = User.builder().age(5).build();User user = User.builder().kid(kid).build();User user1 = User.builder().kid(kid1).build();User user2 = User.builder().kid(kid2).build();User user3 = User.builder().kid(kid3).build();User user4 = User.builder().kid(kid4).build();List list = Arrays.asList(user2,user3,user1,user,user4);System.out.println(list);//Comparator ageDescCom = Comparator.comparing(temp -> temp.getKid().getAge()).reversed();//报错:temp为object//Comparator ageDescCom = Comparator.comparing((User temp) -> temp.getKid().getAge()).reversed();//有效//Comparator ageDescCom = Comparator.comparing(temp -> temp.getKid().getAge(), Comparator.reverseOrder());//有效list = list.stream().sorted(ageDescCom).collect(Collectors.toList());System.out.println(list);}}

二、多层/嵌套/多重条件排序

描述:如:先按照年龄正序,同年龄按照体重倒序。

Comparator A = Comparator.comparing(XXX::getAge).reversed();Comparator B = Comparator.comparing(XXX::getWeight);Comparator C = A.thenComparing(B);

测试代码:

public class ComparatorTest {@Testpublic void test2() {User kid = User.builder().age(4).weight(1).build();User kid1 = User.builder().age(4).weight(2).build();User kid2 = User.builder().age(4).weight(3).build();User kid3 = User.builder().age(4).weight(4).build();User kid4 = User.builder().age(4).weight(5).build();User user = User.builder().age(20).kid(kid).build();User user1 = User.builder().age(20).kid(kid1).build();User user2 = User.builder().age(20).kid(kid2).build();User user3 = User.builder().age(20).kid(kid3).build();User user4 = User.builder().age(20).kid(kid4).build();List list = Arrays.asList(user2,user3,user1,user,user4);System.out.println(list);Comparator userAgeAscCom = Comparator.comparing(User::getAge);Comparator kidAgeAscCom = Comparator.comparing(User::getAge);Comparator kidWeightDescCom = Comparator.comparing(temp -> temp.getKid().getWeight(), Comparator.reverseOrder());Comparator userAgeAscThenKidWeightDescCom = userAgeAscCom.thenComparing(kidWeightDescCom);//父母年龄正序后小孩体重倒序Comparator kidAgeAscThenKidWeightDescCom = kidAgeAscCom.thenComparing(kidWeightDescCom);//小孩年龄正序后小孩体重倒序list = list.stream().sorted(userAgeAscThenKidWeightDescCom).collect(Collectors.toList());System.out.println(list);list = list.stream().sorted(kidAgeAscThenKidWeightDescCom).collect(Collectors.toList());System.out.println(list);}}