B_Osipiuk
B_Osipiuk

Reputation: 1038

Spring pageable force add sorted value

I want to force add sorting parameter to Pageable, even if one is passed. @SortDefault(sort = {"id"}) works well, but only if sorting value is not passed. If is - this annotation is ignored.
How to add "sort by id" even if sorting by other fields is passed? The sorting by ID should be on last place.

Upvotes: 0

Views: 915

Answers (1)

B_Osipiuk
B_Osipiuk

Reputation: 1038

I found a little walkaround that works:

var pageableWithId = PageRequest.of(
        pageable.getPageNumber(),  
        pageable.getPageSize(),
        pageable.getSort().and(Sort.by("id")));

Upvotes: 1

Related Questions