一、数据准备
publicclassOrderTest{private String channelCode;private BigDecimal rate;// 省略 getter、setter、toString()、constructor}
List<OrderTest> orderTestList =newArrayList<>();
OrderTest z09 =newOrderTest("Z09",newBigDecimal("7.6677"));
OrderTest B2C =newOrderTest("B2C",newBigDecimal("5.6666"));
OrderTest Z04 =newOrderTest("Z04",newBigDecimal("4.3137"));
OrderTest ALL =newOrderTest("ALL",newBigDecimal("4.3137"));
OrderTest ALL1 =newOrderTest("ALL",newBigDecimal("4.5137"));// 演示多字段排序使用
OrderTest z091 =newOrderTest("Z09",newBigDecimal("7.6671"));// 演示给 null 值排序用
OrderTest z092 =newOrderTest("Z09", null);
OrderTest B2C1 =newOrderTest("B2C",newBigDecimal("5.6666"));
OrderTest Z041 =newOrderTest("Z04", null);
orderTestList.add(z09);
orderTestList.add(z091);
orderTestList.add(B2C);
orderTestList.add(Z04);
orderTestList.add(ALL);
orderTestList.add(ALL1);
二、单字段排序
2.1、升序
list.stram().sorted(Comparator.Comparing(YourClass::Class's Field)
System.out.println("----------------------------------------------");
System.out.println("只按照 channelCode 升序排序:");
List<OrderTest> channelAsc =
orderTestList.stream().sorted(Comparator.comparing(OrderTest::getChannelCode)).collect(Collectors.toList());
channelAsc.forEach(System.out::println);
结果:
2.2、降序
list.stram().sorted(Comparator.Comparing(YourClass::Class's Field, Comparator.reverseOrder())
System.out.println("----------------------------------------------");
System.out.println("只按照 channelCode 降序排序:");
List<OrderTest> channelDesc =
orderTestList.stream().sorted(Comparator.comparing(OrderTest::getChannelCode, Comparator.reverseOrder())).collect(Collectors.toList());
channelDesc.forEach(System.out::println);
结果:
二、多字段排序
利用的是 thenComparing():
升序 thenComparing(YourClass::Class's Field),
降序 thenComparing(YourClass::Class's Field, Comparator.reverseOrder())。
注意: 使用 thenComparing(YourClass::Class's Field).reversed() 的时候要注意排序要求,
如果先按照 A 字段升序 B 字段升序的话,使用 reversed() 之后的结果是对 A 降序 B 降序。
System.out.println("----------------------------------------------");
System.out.println("先按照 channelCode 升序,再按照 rate 升序排序:");
List<OrderTest> channelCodeAscRateAscList =
orderTestList.stream().sorted(Comparator.comparing(OrderTest::getChannelCode).thenComparing(OrderTest::getRate)).collect(Collectors.toList());
channelCodeAscRateAscList.forEach(System.out::println);
结果
先按照 channelCode 将序,再按照 rate 升序将序,使用 reversed():
System.out.println("----------------------------------------------");
System.out.println("先按照 channelCode 将序,再按照 rate 将序排序,使用 reversed():");
List<OrderTest> channelCodeAscRateAscWithReversedList =
orderTestList.stream().sorted(Comparator.comparing(OrderTest::getChannelCode).thenComparing(OrderTest::getRate).reversed()).collect(Collectors.toList());
channelCodeAscRateAscWithReversedList.forEach(System.out::println);
结果
三、对 null 值处理
Comparator.nullsFirst(Comparator.reverseOrder())-- null排在前面,reverseOrder是倒序,升序用naturalOrder
Comparator.nullsLast(Comparator.reverseOrder())-- null排在后面,reverseOrder是倒序,升序用naturalOrder
orderTestList.add(newOrderTest(("Z09")));
orderTestList.add(newOrderTest(("B2C")));
orderTestList.add(newOrderTest(("Z04")));
System.out.println("----------------------------------------------");
System.out.println("先按照 channelCode 升序,再按照 rate 降序并且 null 值放前面排序:");
List<OrderTest> channelCodeAscRateDescNullFirstList = orderTestList.stream().sorted(Comparator.comparing(OrderTest::getChannelCode).thenComparing(OrderTest::getRate, Comparator.nullsFirst(Comparator.reverseOrder()))).collect(Collectors.toList());
channelCodeAscRateDescNullFirstList.forEach(System.out::println);
结果
四、对排序字段个数不固定的情况,如何排序
需求:排序字段个数不确定,如何实现动态排序?
伪代码示例:
Comparator<SurgUnArrangeResponse> comparing = null;for(int i =0; i < dictItemList.size(); i++){
String cd = dictItemList.get(i).getCd();if(i ==0){// 首次排序
comparing =getComparingByDictCd(cd, null);}else{// 第二次排序及以上
comparing =getComparingByDictCd(cd, comparing);}}// comparing 比较器,保存了排序的规则,responseList 是被排序的集合
responseList = responseList.stream().sorted(comparing).collect(Collectors.toList());// getComparingByDictCd 方法private Comparator<SurgUnArrangeResponse>getComparingByDictCd(String dictCd,
Comparator<SurgUnArrangeResponse> result = null;switch(dictCd){case"age":
result = comparing == null ? Comparator.comparing(SurgUnArrangeResponse::getAge, Comparator.nullsLast(Comparator.reverseOrder())):
comparing.thenComparing(SurgUnArrangeResponse::getAge, Comparator.nullsLast(Comparator.reverseOrder()));break;....}return result;}
本文转载自: https://blog.csdn.net/KevinChen2019/article/details/137017915
版权归原作者 何以解忧,唯有.. 所有, 如有侵权,请联系我们删除。
版权归原作者 何以解忧,唯有.. 所有, 如有侵权,请联系我们删除。