1. Introduction
In this tutorial, weβll learn how to sort query results with Spring Data.
First, weβll take a look at the schema of the data that we want to query and sort. Then weβll discuss how to achieve that with Spring Data.
Letβs get started!
2. The Test Data
Below we have some sample data. Although weβve represented it here as a table, we could use any one of the databases supported by Spring Data to persist it.
The question weβre going to answer is, βWho is occupying which seat on the airline?β To make this more user-friendly, weβll sort by seat number.
| First Name | Last Name | Seat Number |
|---|---|---|
| Jill | Smith | 50 |
| Eve | Jackson | 94 |
| Fred | Bloggs | 22 |
| Ricki | Bobbie | 36 |
| Siya | Kolisi | 85 |
3. Domain
To create a Spring Data Repository, we need to provide a domain class, as well as an id type.
Here weβve modeled our passenger as a JPA entity, but we could have just as easily modeled it as a MongoDB document, or any other model abstraction:
@Entity
class Passenger {
@Id
@GeneratedValue
@Column(nullable = false)
private Long id;
@Basic(optional = false)
@Column(nullable = false)
private String firstName;
@Basic(optional = false)
@Column(nullable = false)
private String lastName;
@Basic(optional = false)
@Column(nullable = false)
private int seatNumber;
// constructor, getters etc.
}
4. Sorting With Spring Data
We have a few different options at our disposal for sorting with Spring Data.
4.1. Sorting With the OrderBy Method Keyword
One option is to use Spring Dataβs method derivation, whereby the query is generated from the method name and signature.
All we need to do here to sort our data is include the keyword OrderBy in our method name, along with the property name(s) and direction (Asc or Desc) by which we want to sort.
We can use this convention to create a query that returns our passengers in ascending order by seat number:
interface PassengerRepository extends JpaRepository<Passenger, Long> {
List<Passenger> findByOrderBySeatNumberAsc();
}
We can also combine this keyword with all the standard Spring Data method names.
Letβs see an example of a method that finds passengers by last name and orders by seat number:
List<Passenger> findByLastNameOrderBySeatNumberAsc(String lastName);
4.2. Sorting With a Sort Parameter
Our second option is to include a Sort parameter specifying the property name(s) and direction by which we want to sort:
List<Passenger> passengers = repository.findAll(Sort.by(Sort.Direction.ASC, "seatNumber"));
In this case, weβre using the findAll() method, and adding the Sort option when calling it.
We can also add this parameter to a new method definition:
List<Passenger> findByLastName(String lastName, Sort sort);
Finally, if perhaps weβre paging, we can specify our sort in a Pageable object:
Page<Passenger> page = repository.findAll(PageRequest.of(0, 1, Sort.by(Sort.Direction.ASC, "seatNumber")));
5. Conclusion
We have two easy options for sorting data with Spring Data: method derivation using the OrderBy keyword, or using the Sort object as a method parameter.
