Java for complex sorting (custom Comparator)
In completing a more complex sorting of a Java List, a custom Comparator is used. Here is a record of the process of use.
public static void main(String[] args) {
List<CardVo> vos = new ArrayList<>();
vos.add(CardVo.builder().batchType(TicketBatchTypeEnum.TONG_TICKET).expiredTime(LocalDateTime.of(2021, 3, 31, 0, 0, 0)).faceValue(BigDecimal.valueOf(50)).build());
vos.add(CardVo.builder().batchType(TicketBatchTypeEnum.TONG_TICKET).expiredTime(LocalDateTime.of(2021, 3, 30, 0, 0, 0)).faceValue(BigDecimal.valueOf(40)).build());
vos.add(CardVo.builder().batchType(TicketBatchTypeEnum.TONG_TICKET).expiredTime(LocalDateTime.of(2021, 3, 29, 0, 0, 0)).faceValue(BigDecimal.valueOf(30)).build());
vos.add(CardVo.builder().batchType(TicketBatchTypeEnum.TONG_TICKET).expiredTime(LocalDateTime.of(2021, 3, 27, 0, 0, 0)).faceValue(BigDecimal.valueOf(20)).build());
vos.add(CardVo.builder().batchType(TicketBatchTypeEnum.TONG_TICKET).expiredTime(LocalDateTime.of(2021, 3, 27, 0, 0, 0)).faceValue(BigDecimal.valueOf(10)).build());
vos.add(CardVo.builder().batchType(TicketBatchTypeEnum.PRICE_TICKET).expiredTime(LocalDateTime.of(2021, 3, 31, 0, 0, 0)).faceValue(BigDecimal.valueOf(50)).build());
vos.add(CardVo.builder().batchType(TicketBatchTypeEnum.PRICE_TICKET).expiredTime(LocalDateTime.of(2021, 3, 30, 0, 0, 0)).faceValue(BigDecimal.valueOf(40)).build());
vos.add(CardVo.builder().batchType(TicketBatchTypeEnum.PRICE_TICKET).expiredTime(LocalDateTime.of(2021, 3, 29, 0, 0, 0)).faceValue(BigDecimal.valueOf(30)).build());
vos.add(CardVo.builder().batchType(TicketBatchTypeEnum.PRICE_TICKET).expiredTime(LocalDateTime.of(2021, 3, 27, 0, 0, 0)).faceValue(BigDecimal.valueOf(20)).build());
vos.add(CardVo.builder().batchType(TicketBatchTypeEnum.PRICE_TICKET).expiredTime(LocalDateTime.of(2021, 3, 27, 0, 0, 0)).faceValue(BigDecimal.valueOf(10)).build());
// 排序前的list
String s = JSONObject.toJSONString(vos);
System.out.println(s);
// 排序后的list
List<CardVo> cardVoComparator = orderBy(vos);
s = JSONObject.toJSONString(cardVoComparator);
System.out.println(s);
}
/**
* 排序方法
*
* @param cardVos 排序前的List
* @return 排序后的List
*/
private static List<CardVo> orderBy(List<CardVo> cardVos) {
ComparatorCardVo comparatorCardVo = new ComparatorCardVo();
cardVos = cardVos.stream().sorted(comparatorCardVo).collect(Collectors.toList());
return cardVos;
}
result
排序前的:
[{"batchType":"TONG_TICKET","expiredTime":"2021-03-31T00:00:00","faceValue":50},{"batchType":"TONG_TICKET","expiredTime":"2021-03-30T00:00:00","faceValue":40},{"batchType":"TONG_TICKET","expiredTime":"2021-03-29T00:00:00","faceValue":30},{"batchType":"TONG_TICKET","expiredTime":"2021-03-27T00:00:00","faceValue":20},{"batchType":"TONG_TICKET","expiredTime":"2021-03-27T00:00:00","faceValue":10},{"batchType":"PRICE_TICKET","expiredTime":"2021-03-31T00:00:00","faceValue":50},{"batchType":"PRICE_TICKET","expiredTime":"2021-03-30T00:00:00","faceValue":40},{"batchType":"PRICE_TICKET","expiredTime":"2021-03-29T00:00:00","faceValue":30},{"batchType":"PRICE_TICKET","expiredTime":"2021-03-27T00:00:00","faceValue":20},{"batchType":"PRICE_TICKET","expiredTime":"2021-03-27T00:00:00","faceValue":10}]
排序后的:
[{"batchType":"TONG_TICKET","expiredTime":"2021-03-27T00:00:00","faceValue":20},{"batchType":"TONG_TICKET","expiredTime":"2021-03-27T00:00:00","faceValue":10},{"batchType":"TONG_TICKET","expiredTime":"2021-03-29T00:00:00","faceValue":30},{"batchType":"TONG_TICKET","expiredTime":"2021-03-30T00:00:00","faceValue":40},{"batchType":"TONG_TICKET","expiredTime":"2021-03-31T00:00:00","faceValue":50},{"batchType":"PRICE_TICKET","expiredTime":"2021-03-27T00:00:00","faceValue":20},{"batchType":"PRICE_TICKET","expiredTime":"2021-03-27T00:00:00","faceValue":10},{"batchType":"PRICE_TICKET","expiredTime":"2021-03-29T00:00:00","faceValue":30},{"batchType":"PRICE_TICKET","expiredTime":"2021-03-30T00:00:00","faceValue":40},{"batchType":"PRICE_TICKET","expiredTime":"2021-03-31T00:00:00","faceValue":50}]
Custom Comparator
/**
* @Description: <br/>
* 优化券排序
* <p>
* <br/>
* @Author: zhangqi
* @create 2021/3/26 12:59
*/
@NoArgsConstructor
public class ComparatorCardVo implements Comparator<CardVo> {
/**
* 优化券排序规则
*
* @param o1 参数1
* @param o2 参数2
* @return 比较结果
*/
@Override
public int compare(CardVo o1, CardVo o2) {
// 比较券类型 通兑券在上
TicketBatchTypeEnum batchType1 = o1.getBatchType();
TicketBatchTypeEnum batchType2 = o2.getBatchType();
// 券类型不相同 通兑券在上
if (!Objects.equals(batchType1, batchType2)) {
return !Objects.equals(batchType1, TicketBatchTypeEnum.TONG_TICKET) ? 1 : -1;
}
// 比较券时间 时间小的在上
LocalDateTime expiredTime1 = o1.getExpiredTime();
LocalDateTime expiredTime2 = o2.getExpiredTime();
// 卷类型相同 时间小的在上
if (!Objects.equals(expiredTime1, expiredTime2)) {
return expiredTime1.compareTo(expiredTime2) < 0 ? -1 : 1;
}
// 比较金额
BigDecimal faceValue1 = o1.getFaceValue();
BigDecimal faceValue2 = o2.getFaceValue();
// 卷类型相同 券时间相同 金额大的在上
if (!BigDecimalUtil.compare(faceValue1, CompareEnum.EQ, faceValue2)) {
return BigDecimalUtil.compare(faceValue1, CompareEnum.GT, faceValue2) ? -1 : 1;
}
return 0;
}
}