Spring MVC @SortDefault multi-field sorting

In SpringMVC, we can use @SortDefault or @PageableDefault to quickly complete the default sorting of Pageable

Most of what we can query is to sort a certain field:

@PageableDefault(sort = {"weight"}, direction = Sort.Direction.DESC)
                                 Pageable pageable
@SortDefault(sort = {"weight"}, direction = Sort.Direction.ASC) Pageable pageable

Or sort certain fields, but the sorting rules are the same (for example, all are in ascending order):

@SortDefault(sort = {"weight", "createTime"}, direction = Sort.Direction.ASC) Pageable pageable

If we want to set multiple sort fields, and the sorting method of each sort field is not consistent, we can use the following code:

  public Page<Notice> page(@SortDefault.SortDefaults({
      @SortDefault(sort = {"weight"}, direction = Sort.Direction.ASC),
      @SortDefault(sort = {"createTime"}, direction = Sort.Direction.DESC)})
                                 Pageable pageable) {

At this point, the above code will be weight in ascending order by 061b15c2346dad, and then sorted in reverse order by createTime.

2.6k 声望
203 粉丝
0 条评论
2.6k 声望
203 粉丝