Search code examples
javaspringspring-data-jpapageable

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.


Solution

  • I found a little walkaround that works:

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