Spring Data Rest - Sort by multiple properties
Solution 1
Solution (tl;dr)
When wanting to sort on multiple fields you simply put the sort
parameter multiple times in the URI. For example your/uri?sort=name,asc&sort=numberOfHands,desc
. Spring Data is then capable of constructing a Pageable
object with multiple sorts.
Explanation
There is not really a defined standard on how to submit multiple values for a parameter in a URI. See Correct way to pass multiple values for same parameter name in GET request.
However there is some information in the Java Servlet Spec which hints on how Java servlet containers parse request parameters.
The
getParameterValues
method returns an array ofString
objects containing all the parameter values associated with a parameter name. ... - Java Servlet Spec, section 3.1
The sample further in that section states (although it mixes request and body data)
For example, if a request is made with a query string of
a=hello
and a post body ofa=goodbye&a=world
, the resulting parameter set would be ordereda=hello, goodbye, world
.
This sample shows that when a parameter (a
in the example) is presented multiple times the results will be aggregated into a String[]
.
Solution 2
Here is how to construct the multi Sort object manually/programatically.
Sort sort = Sort.by(
Sort.Order.asc("name"),
Sort.Order.desc("numberOfHands"));
return personRepository.findAll(sort);
Note: This solution does not directly solve the original question asked, but may help visitors that landed on this question while searching for a solution how to sort on multiple properties from a backend perspective / in a somewhat "hardcoded" way. (this solution does not require/take any URI parameters)
rakpan
Updated on January 15, 2022Comments
-
rakpan over 2 years
I have an entity as below
Class Person{ String id; String name; String numberOfHands; }
With Spring Data Rest (Gosling Release Train), I'm able to specify
localhost/Person?sort=name,asc
for sorting name name ascending. Now, in a case where I need to sort by numberOfHands descending and name ascending. I'm able to specify
localhost/Person?sort=numberOfHands,name,asc
But, I'm not able to specify
localhost/Person?sort=numberOfHands,desc,name,asc
Is there a way to specify multiple sort order?
Thanks!
-
aboger over 5 yearsInteresting, though I guess this will only work on the server side.
-
BitfulByte over 5 yearsYes, this works when you need to "hardcode" it server side. It uses the Sort class from Spring data. I have clarified the answer so it's hopefully more clear now :-)
-
padmanabhanm almost 5 yearsSay I use JPQL and join across multiple tables. And I want to sort it based on two columns. One from the first table and another from second table. if I pass it as
Sort.by(Sort.Direction.ASC,"first_table_col","second_table_col")
, I get an exception saying "second_table_col" not found in First_table. Is it possible ? -
BitfulByte almost 5 years@iamL perhaps try with the prefix of the second table?
-
padmanabhanm almost 5 years@PimHazebroek .. That is what I ended up doing. Cleared alias names for the tables and it worked. Thanks.
-
Seldo97 almost 4 yearsSecond way to construct multi sort in repository:
return this.bookRepository.findAll(Sort.by("published").descending().and(Sort.by("title")).a cending());
-
sasynkamil about 3 years