The Sort class in Spring Data (org.springframework.data.domain.Sort) describes how query results should be ordered: which properties to sort by and in which direction. You pass it to repository methods or wrap it in a Pageable, and Spring Data translates it into the ORDER BY clause (or the equivalent) of the underlying store.
It is store-neutral, so the same Sort works with Spring Data JPA, MongoDB, JDBC and others. Because it works on property names rather than column names, it keeps sorting logic out of hand-written query strings.
A Sort is an immutable value object made of one or more Sort.Order entries. The simplest factory methods are:
Methods such as ascending(), descending() and and(...) return a new instance. Since Sort is immutable, you must use the returned value.
A repository method can accept a Sort as its last parameter. Spring Data applies it to derived queries and to @Query methods that use JPQL or derived-style queries.
JpaRepository and ListCrudRepository with ListPagingAndSortingRepository also provide findAll(Sort sort) out of the box:
You can also fix the order in the method name, for example findByStatusOrderByCreatedAtDesc(String status). Use that only when the order never changes; use a Sort parameter when callers decide.
Sort is usually combined with paging through PageRequest:
In a Spring MVC controller you can let Spring resolve the sort from the query string. With Spring Data's web support (@EnableSpringDataWebSupport, enabled automatically by Spring Boot), a request like GET /customers?page=0&size=20&sort=lastName,desc becomes a Pageable:
Always validate or whitelist the sortable properties exposed to clients. An unknown property raises PropertyReferenceException, and unrestricted sorting on unindexed columns can be a performance problem.
Each Sort.Order carries more than direction.
| Option | Method | Purpose |
|---|---|---|
| Direction | Order.asc(...), Order.desc(...) | Ascending or descending |
| Ignore case | ignoreCase() | Case-insensitive ordering, such as lower(name) |
| Null handling | nullsFirst(), nullsLast(), nullsNative() | Where NULL values appear |
Support for null handling and case-insensitivity depends on the store and on the query type.
For compile-time safety, Sort.TypedSort lets you reference properties through method references:
The type-safe form needs the property to be reachable through getters on a non-final class and relies on proxying, so it fits plain entity classes better than records. For sorting on aggregated or computed values in @Query methods, use JpaSort.unsafe("LENGTH(name)"). Only pass trusted strings to it, never raw user input.
Ascending. Sort.by("name") is the same as Sort.by(Sort.Direction.ASC, "name").
Yes, use dot notation such as Sort.by("address.city"). Spring Data resolves the path and adds the needed join for JPA.
Sort only defines ordering. Pageable adds page number and size and can carry a Sort. Use Pageable when you also need to limit results.
The name passed to Sort is not a property of the entity. Sort by the Java property name, not the column name.
Sort is Spring Data's portable, immutable description of result ordering. Build it with Sort.by, pass it directly to repository methods or wrap it in a PageRequest, and use Sort.Order for case and null handling. Whitelist client-supplied sort fields and keep sorted columns indexed.