Spring Data Rest - Sort by multiple properties











up vote
35
down vote

favorite
10












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!










share|improve this question


















  • 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















up vote
35
down vote

favorite
10












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!










share|improve this question


















  • 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













up vote
35
down vote

favorite
10









up vote
35
down vote

favorite
10






10





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!










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 8 '15 at 14:05









rakpan

1,06211729




1,06211729








  • 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














  • 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








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












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 of String 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 of a=goodbye&a=world, the resulting parameter set would be ordered a=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.






share|improve this answer






























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





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      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

























      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 of String 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 of a=goodbye&a=world, the resulting parameter set would be ordered a=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.






      share|improve this answer



























        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 of String 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 of a=goodbye&a=world, the resulting parameter set would be ordered a=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.






        share|improve this answer

























          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 of String 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 of a=goodbye&a=world, the resulting parameter set would be ordered a=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.






          share|improve this answer














          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 of String 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 of a=goodbye&a=world, the resulting parameter set would be ordered a=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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 23 '17 at 12:17









          Community

          11




          11










          answered Oct 9 '15 at 9:21









          M. Deinum

          66.7k11136147




          66.7k11136147
























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





              share|improve this answer

























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





                share|improve this answer























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





                  share|improve this answer












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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 14 at 21:11









                  Pim Hazebroek

                  1,2071221




                  1,2071221






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python