To sort paginated results in Spring Data JPA, build a Pageable with PageRequest.of(page, size, Sort) and pass it to a repository method that returns Page<T>. Spring Data applies the ORDER BY, LIMIT and OFFSET in a single query, and also runs a count query so the Page knows the total number of elements.
Sorting is part of the Pageable, so you never need a separate Sort parameter next to it. The same approach works for derived queries, @Query methods, and Spring MVC controllers that accept a Pageable directly.
A Pageable holds the zero-based page index, the page size and an optional Sort. PageRequest is the standard implementation. A Sort is a list of Sort.Order objects, each with a property name and a direction.
| Type | Purpose |
|---|---|
Pageable | Page number, page size and sort in one object |
PageRequest | Concrete Pageable created with PageRequest.of(...) |
Sort | One or more Sort.Order entries (property + direction) |
Page<T> | Content, total elements, total pages and the sort that was applied |
Slice<T> | Like Page, but without the count query |
The repository only needs to extend JpaRepository (which already extends PagingAndSortingRepository behaviour for Pageable arguments).
Since Spring Boot 3 the persistence annotations live in the jakarta.persistence package, not javax.persistence.
The overload PageRequest.of(page, size, sort) takes a Sort. The page index starts at 0.
findAll(Pageable) returns every row, ten at a time, ordered by price from high to low. findByCategory filters first, then sorts and slices. There is a shortcut for single properties: PageRequest.of(0, 10, Sort.Direction.DESC, "price").
Chain Sort objects with and, or build the orders explicitly. Earlier orders take priority.
You can also write Sort.by("category").ascending().and(Sort.by("price").descending()), which produces the same result.
Use Slice<Product> as the return type when you only need "is there a next page" (infinite scroll, for example). It skips the count query, which matters on large tables.
Spring Data's web support resolves Pageable from request parameters, so the client chooses the page and sort.
A request such as the following sorts by price descending, then by name ascending:
Spring Boot limits the maximum page size (default 2000) through spring.data.web.pageable.max-page-size. Clients can pass any property name in sort, so a property that does not exist on the entity raises a PropertyReferenceException. Validate or whitelist sort fields if the endpoint is public.
With JPQL, Spring Data appends the sort from the Pageable automatically.
For native queries, dynamic sorting through Pageable is not applied reliably because Spring Data cannot rewrite arbitrary SQL. Write the ORDER BY in the SQL itself, or use a JPQL query, a derived query, or a Specification instead. A native paginated query also needs an explicit countQuery attribute.
Sort.by("Name") with the wrong case: property names are the Java field names, not column names.id as the last sort order.OFFSET pagination gets slower on deep pages; for very large tables consider keyset pagination with ScrollPosition, available in Spring Data 3.1 and later.@EntityGraph on single-valued associations.No. Put the Sort inside the PageRequest and pass only the Pageable. Passing both is not supported for the same method.
Use the property path with a dot, for example Sort.by("category.name"). Spring Data joins the association for you.
Page runs an extra count query to know the total, while Slice only checks whether a next slice exists. Choose Slice when you do not show page numbers.
Use Sort.Order.asc("name").ignoreCase(). Spring Data wraps the property in lower() in the generated query.
Create a Pageable with PageRequest.of(page, size, sort), return Page<T> or Slice<T> from the repository, and let Spring Data add the ORDER BY, LIMIT and OFFSET. In controllers, accept Pageable directly and restrict the sortable fields. Add a unique tie-breaker to keep pages stable.