Spring Data Rest - Sort by multiple properties
up vote
35
down vote
favorite
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!
spring spring-data spring-data-jpa spring-data-rest
add a comment |
up vote
35
down vote
favorite
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!
spring spring-data spring-data-jpa spring-data-rest
3
Add multiplesort
attributes.sort=name,asc&sort=numberOfHands,desc
.
– M. Deinum
Oct 8 '15 at 14:14
Great! that works.. But that is weird.. Except for a string being appended to URL, I'm not sure if there is a direct way of generating such URL through JavaScript.. Please let me know if I'm wrong..
– rakpan
Oct 9 '15 at 3:01
Why would it be weird that is just the way the web (or URLs) work. You can generate whatever URL you like with javascript. See stackoverflow.com/questions/24059773/….
– M. Deinum
Oct 9 '15 at 5:47
1
@M.Deinum - You might wanna turn this into an answer as it's correct. If you have trouble creating such URIs via JavaScript, blame JavaScript or the library you're using. :)
– Oliver Drotbohm
Oct 9 '15 at 8:43
add a comment |
up vote
35
down vote
favorite
up vote
35
down vote
favorite
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!
spring spring-data spring-data-jpa spring-data-rest
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!
spring spring-data spring-data-jpa spring-data-rest
spring spring-data spring-data-jpa spring-data-rest
asked Oct 8 '15 at 14:05
rakpan
1,06211729
1,06211729
3
Add multiplesort
attributes.sort=name,asc&sort=numberOfHands,desc
.
– M. Deinum
Oct 8 '15 at 14:14
Great! that works.. But that is weird.. Except for a string being appended to URL, I'm not sure if there is a direct way of generating such URL through JavaScript.. Please let me know if I'm wrong..
– rakpan
Oct 9 '15 at 3:01
Why would it be weird that is just the way the web (or URLs) work. You can generate whatever URL you like with javascript. See stackoverflow.com/questions/24059773/….
– M. Deinum
Oct 9 '15 at 5:47
1
@M.Deinum - You might wanna turn this into an answer as it's correct. If you have trouble creating such URIs via JavaScript, blame JavaScript or the library you're using. :)
– Oliver Drotbohm
Oct 9 '15 at 8:43
add a comment |
3
Add multiplesort
attributes.sort=name,asc&sort=numberOfHands,desc
.
– M. Deinum
Oct 8 '15 at 14:14
Great! that works.. But that is weird.. Except for a string being appended to URL, I'm not sure if there is a direct way of generating such URL through JavaScript.. Please let me know if I'm wrong..
– rakpan
Oct 9 '15 at 3:01
Why would it be weird that is just the way the web (or URLs) work. You can generate whatever URL you like with javascript. See stackoverflow.com/questions/24059773/….
– M. Deinum
Oct 9 '15 at 5:47
1
@M.Deinum - You might wanna turn this into an answer as it's correct. If you have trouble creating such URIs via JavaScript, blame JavaScript or the library you're using. :)
– Oliver Drotbohm
Oct 9 '15 at 8:43
3
3
Add multiple
sort
attributes. sort=name,asc&sort=numberOfHands,desc
.– M. Deinum
Oct 8 '15 at 14:14
Add multiple
sort
attributes. sort=name,asc&sort=numberOfHands,desc
.– M. Deinum
Oct 8 '15 at 14:14
Great! that works.. But that is weird.. Except for a string being appended to URL, I'm not sure if there is a direct way of generating such URL through JavaScript.. Please let me know if I'm wrong..
– rakpan
Oct 9 '15 at 3:01
Great! that works.. But that is weird.. Except for a string being appended to URL, I'm not sure if there is a direct way of generating such URL through JavaScript.. Please let me know if I'm wrong..
– rakpan
Oct 9 '15 at 3:01
Why would it be weird that is just the way the web (or URLs) work. You can generate whatever URL you like with javascript. See stackoverflow.com/questions/24059773/….
– M. Deinum
Oct 9 '15 at 5:47
Why would it be weird that is just the way the web (or URLs) work. You can generate whatever URL you like with javascript. See stackoverflow.com/questions/24059773/….
– M. Deinum
Oct 9 '15 at 5:47
1
1
@M.Deinum - You might wanna turn this into an answer as it's correct. If you have trouble creating such URIs via JavaScript, blame JavaScript or the library you're using. :)
– Oliver Drotbohm
Oct 9 '15 at 8:43
@M.Deinum - You might wanna turn this into an answer as it's correct. If you have trouble creating such URIs via JavaScript, blame JavaScript or the library you're using. :)
– Oliver Drotbohm
Oct 9 '15 at 8:43
add a comment |
2 Answers
2
active
oldest
votes
up vote
53
down vote
accepted
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
.
add a comment |
up vote
5
down vote
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);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
53
down vote
accepted
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
.
add a comment |
up vote
53
down vote
accepted
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
.
add a comment |
up vote
53
down vote
accepted
up vote
53
down vote
accepted
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 (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
.
edited May 23 '17 at 12:17
Community♦
11
11
answered Oct 9 '15 at 9:21
M. Deinum
66.7k11136147
66.7k11136147
add a comment |
add a comment |
up vote
5
down vote
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);
add a comment |
up vote
5
down vote
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);
add a comment |
up vote
5
down vote
up vote
5
down vote
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);
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);
answered Jan 14 at 21:11
Pim Hazebroek
1,2071221
1,2071221
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33018127%2fspring-data-rest-sort-by-multiple-properties%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Add multiple
sort
attributes.sort=name,asc&sort=numberOfHands,desc
.– M. Deinum
Oct 8 '15 at 14:14
Great! that works.. But that is weird.. Except for a string being appended to URL, I'm not sure if there is a direct way of generating such URL through JavaScript.. Please let me know if I'm wrong..
– rakpan
Oct 9 '15 at 3:01
Why would it be weird that is just the way the web (or URLs) work. You can generate whatever URL you like with javascript. See stackoverflow.com/questions/24059773/….
– M. Deinum
Oct 9 '15 at 5:47
1
@M.Deinum - You might wanna turn this into an answer as it's correct. If you have trouble creating such URIs via JavaScript, blame JavaScript or the library you're using. :)
– Oliver Drotbohm
Oct 9 '15 at 8:43