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:

Customers listing

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):

Sort customers by multiple columns 1

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:

Sort customers by multiple columns 2

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:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment