需求:查询数据库中数据按top正序排列,如果top相同就按日期的倒序进行排列
第一种,从数据库中查询出来是个集合,然后通过stream().sorted()进行排序
Set<GiftPlanDTO> resGiftPlan = giftPlanDAO.findAllByUserId(JwtExtractor.extractUserId()).stream()
.map(mapper::toDto).collect(Collectors.toSet());
return resGiftPlan.stream().
sorted(Comparator.comparingInt(GiftPlanDTO::getTop).
thenComparing(GiftPlanDTO::getGiftDate, Comparator.reverseOrder())).
collect(Collectors.toCollection(LinkedHashSet::new));
第二种,通过JPA的Pageable在controller上的加注解实现
@SortDefault.SortDefaults({
@SortDefault(sort = {"top"}, direction = Sort.Direction.ASC),
@SortDefault(sort = {"giftDate"}, direction = Sort.Direction.DESC)}) Pageable pageable
网友评论