java 排序多个条件

Comparator<RecentReceivingAccountVO> byTime= Comparator.comparing(RecentReceivingAccountVO::getCreateTime);
        Comparator<RecentReceivingAccountVO> byName = Comparator.comparing(p->p.getAccountName().equals(user.getNickName()));
        return voList.stream().sorted(byName.reversed().thenComparing(byTime.reversed())).collect(Collectors.toList());

 

  //按照总价,再按照物流,再按照创建时间
        Comparator<RmpAutoQuotationResourcePriceVO> byTotalPrice = Comparator.comparing(RmpAutoQuotationResourcePriceVO::getTotalPrice);
        Comparator<RmpAutoQuotationResourcePriceVO> byLgisticsPrice = Comparator.comparing(RmpAutoQuotationResourcePriceVO::getLogisticsPrice);
        Comparator<RmpAutoQuotationResourcePriceVO> byCreateTime = Comparator.comparing(RmpAutoQuotationResourcePriceVO::getCreateTime);
        //有物流信息只返回价格最低
        Optional<RmpAutoQuotationResourcePriceVO> hasLogistics = resourcePriceVOList.stream().
                filter(RmpAutoQuotationResourcePriceVO::isHasLogistics).
                sorted(byTotalPrice.thenComparing(byLgisticsPrice).thenComparing(byCreateTime)).findFirst();
        if (hasLogistics.isPresent()) {
            autoPriceQuo.add(copyToAutoPriceQuo(hasLogistics.get(),cityCode));
        }

 

posted @ 2022-03-01 17:04  八英里  阅读(359)  评论(0编辑  收藏  举报