Spring Data JPA Sorting by Multiple Columns Examples
- Details
- Written by Nam Ha Minh
- Last Updated on 27 March 2023   |   Print Email
You know, Spring Data JPA makes it easy to sort a list of entities, as demonstrated in the article Spring Data JPA Paging and Sorting Examples. And what about sorting by multiple columns?
For example, the following Customers listing is sorted by country name, then sorted by State, then sorted by City - all in ascending order:
Suppose that we have code of entity class Customer as follows:
package net.codejava @Entity @Table(name = "customers") public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) protected Integer id; private String email; private String password; @Column(name = "first_name") protected String firstName; @Column(name = "last_name") protected String lastName; @ManyToOne @JoinColumn(name = "country_id") protected Country country; protected String city; protected String state; // getters and setters are not shown... }
And code of the Country class:
@Entity @Table(name = "countries") public class Country { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; // getters and setters are not shown... }
And code of the repository interface that extends PagingAndSortingRepository defined by Spring Data JPA:
public interface CustomerRepository extends PagingAndSortingRepository<Customer, Integer> { }
The following code is used to get a list of Customer entities sorted by a single field “firstName” in ascending order:
Sort sort = Sort.by("firstName").ascending(); Iterable<Customer> customers = repo.findAll(sort);
And to query a collection of Customer entities sorted by country name and then by state, use the following code:
Sort sort = Sort.by("country_name").ascending() .and(Sort.by("state").ascending()); Iterable<Customer> customers = repo.findAll(sort); customers.forEach(c -> { System.out.printf("%-30s - %-20s - %-20s\n", c.getFirstName() + " " + c.getLastName(), c.getCountryName(), c.getState()); });
You can notice “customer_name” is a nested property. See Spring Data JPA Sort by Nested Property article.
And it will produce the following output (sample data):
And the following code example gets a list of customers sorted firstly by country, then by state and finally by city:
Sort sort = Sort.by("country_name").ascending(); sort = sort.and(Sort.by("state").ascending()); sort = sort.and(Sort.by("city").ascending()); Iterable<Customer> customers = repo.findAll(sort); customers.forEach(c -> { System.out.printf("%-30s - %-20s - %-20s - %-20s\n", c.getFirstName() + " " + c.getLastName(), c.getCountryName(), c.getState(), c.getCity()); });
And below is a sample output:
NOTE: You can use the methods ascending() and descending() to specify the sort direction by each field separately.
So, as you can see, Spring Data JPA makes it easy to write code for sorting data by multiple columns. I hope you find the code examples in this article helpful.
Watch the following video to see the coding in action:
Related Spring Data JPA Tutorials:
- Spring Data JPA Paging and Sorting Examples
- Spring Data JPA Filter Search Examples
- Spring Boot Full Text Search with MySQL Database Tutorial
- Spring Data JPA EntityManager Examples (CRUD Operations)
- JPA EntityManager: Understand Differences between Persist and Merge
- Understand Spring Data JPA with Simple Example
- Spring Data JPA Custom Repository Example
Comments