How do I sort an array in bash?












2














I have an array with h4 h5 h1 h2 h3 in it and I would like to sort it according to the numbers, but don't know how. What is the best way to do this?



edit1: I would also like to sort an array via the numbers containing different letters, for example s4 h5 q1 h2 g3.










share|improve this question
























  • That's a string; do you have that, exactly, or an actual array?
    – Jeff Schaller
    14 hours ago










  • i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
    – Mercyfon
    14 hours ago






  • 1




    Possible duplicate of How to create a function that can sort an array in bash?
    – roaima
    8 hours ago
















2














I have an array with h4 h5 h1 h2 h3 in it and I would like to sort it according to the numbers, but don't know how. What is the best way to do this?



edit1: I would also like to sort an array via the numbers containing different letters, for example s4 h5 q1 h2 g3.










share|improve this question
























  • That's a string; do you have that, exactly, or an actual array?
    – Jeff Schaller
    14 hours ago










  • i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
    – Mercyfon
    14 hours ago






  • 1




    Possible duplicate of How to create a function that can sort an array in bash?
    – roaima
    8 hours ago














2












2








2







I have an array with h4 h5 h1 h2 h3 in it and I would like to sort it according to the numbers, but don't know how. What is the best way to do this?



edit1: I would also like to sort an array via the numbers containing different letters, for example s4 h5 q1 h2 g3.










share|improve this question















I have an array with h4 h5 h1 h2 h3 in it and I would like to sort it according to the numbers, but don't know how. What is the best way to do this?



edit1: I would also like to sort an array via the numbers containing different letters, for example s4 h5 q1 h2 g3.







bash sort array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 13 hours ago









Jeff Schaller

38.5k1053125




38.5k1053125










asked 14 hours ago









Mercyfon

184




184












  • That's a string; do you have that, exactly, or an actual array?
    – Jeff Schaller
    14 hours ago










  • i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
    – Mercyfon
    14 hours ago






  • 1




    Possible duplicate of How to create a function that can sort an array in bash?
    – roaima
    8 hours ago


















  • That's a string; do you have that, exactly, or an actual array?
    – Jeff Schaller
    14 hours ago










  • i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
    – Mercyfon
    14 hours ago






  • 1




    Possible duplicate of How to create a function that can sort an array in bash?
    – roaima
    8 hours ago
















That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
14 hours ago




That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
14 hours ago












i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
14 hours ago




i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
14 hours ago




1




1




Possible duplicate of How to create a function that can sort an array in bash?
– roaima
8 hours ago




Possible duplicate of How to create a function that can sort an array in bash?
– roaima
8 hours ago










3 Answers
3






active

oldest

votes


















2














Try this,



Just print, sort and store the values in the same array name.



ary=(h4 h5 h1 h2 h3)
ary=(`printf '%sn' "${ary[@]}"|sort`)

echo ${ary[@]}
h1 h2 h3 h4 h5





share|improve this answer





























    2














    No need to use tr; shell's "Parameter Expansion" with an adequate IFS (in a subshell) should suffice. Try



    $ ARR=(h4 h5 h1 h2 h3)
    $ SA=( $(IFS=$'n'; echo "${ARR[*]}" | sort) )
    $ BRR=(s4 h5 q1 h2 g3)
    $ SB=( $(IFS=$'n'; echo "${BRR[*]}" | sort -k1.2) )
    $ echo "${SB[*]}"
    q1 h2 g3 s4 h5





    share|improve this answer





















    • This would be better if echo was substituted for printf
      – D. Ben Knoble
      6 hours ago



















    1














    Lets take an array A as



    A=(h4 h5 h1 h2 h3)


    Now, the problem with the sort command is that it sorts elements in different line and can't sort elements in the same line. So, the workaround is to transform the array into an element per line and sort with sort and put them in an array which is actually sorted, that is,



    B=(`echo ${A[@]} | tr " " "n" | sort`)


    Now, B is the sorted array. Here, tr transforms space into a newline






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2funix.stackexchange.com%2fquestions%2f490992%2fhow-do-i-sort-an-array-in-bash%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Try this,



      Just print, sort and store the values in the same array name.



      ary=(h4 h5 h1 h2 h3)
      ary=(`printf '%sn' "${ary[@]}"|sort`)

      echo ${ary[@]}
      h1 h2 h3 h4 h5





      share|improve this answer


























        2














        Try this,



        Just print, sort and store the values in the same array name.



        ary=(h4 h5 h1 h2 h3)
        ary=(`printf '%sn' "${ary[@]}"|sort`)

        echo ${ary[@]}
        h1 h2 h3 h4 h5





        share|improve this answer
























          2












          2








          2






          Try this,



          Just print, sort and store the values in the same array name.



          ary=(h4 h5 h1 h2 h3)
          ary=(`printf '%sn' "${ary[@]}"|sort`)

          echo ${ary[@]}
          h1 h2 h3 h4 h5





          share|improve this answer












          Try this,



          Just print, sort and store the values in the same array name.



          ary=(h4 h5 h1 h2 h3)
          ary=(`printf '%sn' "${ary[@]}"|sort`)

          echo ${ary[@]}
          h1 h2 h3 h4 h5






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 14 hours ago









          msp9011

          3,71743863




          3,71743863

























              2














              No need to use tr; shell's "Parameter Expansion" with an adequate IFS (in a subshell) should suffice. Try



              $ ARR=(h4 h5 h1 h2 h3)
              $ SA=( $(IFS=$'n'; echo "${ARR[*]}" | sort) )
              $ BRR=(s4 h5 q1 h2 g3)
              $ SB=( $(IFS=$'n'; echo "${BRR[*]}" | sort -k1.2) )
              $ echo "${SB[*]}"
              q1 h2 g3 s4 h5





              share|improve this answer





















              • This would be better if echo was substituted for printf
                – D. Ben Knoble
                6 hours ago
















              2














              No need to use tr; shell's "Parameter Expansion" with an adequate IFS (in a subshell) should suffice. Try



              $ ARR=(h4 h5 h1 h2 h3)
              $ SA=( $(IFS=$'n'; echo "${ARR[*]}" | sort) )
              $ BRR=(s4 h5 q1 h2 g3)
              $ SB=( $(IFS=$'n'; echo "${BRR[*]}" | sort -k1.2) )
              $ echo "${SB[*]}"
              q1 h2 g3 s4 h5





              share|improve this answer





















              • This would be better if echo was substituted for printf
                – D. Ben Knoble
                6 hours ago














              2












              2








              2






              No need to use tr; shell's "Parameter Expansion" with an adequate IFS (in a subshell) should suffice. Try



              $ ARR=(h4 h5 h1 h2 h3)
              $ SA=( $(IFS=$'n'; echo "${ARR[*]}" | sort) )
              $ BRR=(s4 h5 q1 h2 g3)
              $ SB=( $(IFS=$'n'; echo "${BRR[*]}" | sort -k1.2) )
              $ echo "${SB[*]}"
              q1 h2 g3 s4 h5





              share|improve this answer












              No need to use tr; shell's "Parameter Expansion" with an adequate IFS (in a subshell) should suffice. Try



              $ ARR=(h4 h5 h1 h2 h3)
              $ SA=( $(IFS=$'n'; echo "${ARR[*]}" | sort) )
              $ BRR=(s4 h5 q1 h2 g3)
              $ SB=( $(IFS=$'n'; echo "${BRR[*]}" | sort -k1.2) )
              $ echo "${SB[*]}"
              q1 h2 g3 s4 h5






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 11 hours ago









              RudiC

              4,1491312




              4,1491312












              • This would be better if echo was substituted for printf
                – D. Ben Knoble
                6 hours ago


















              • This would be better if echo was substituted for printf
                – D. Ben Knoble
                6 hours ago
















              This would be better if echo was substituted for printf
              – D. Ben Knoble
              6 hours ago




              This would be better if echo was substituted for printf
              – D. Ben Knoble
              6 hours ago











              1














              Lets take an array A as



              A=(h4 h5 h1 h2 h3)


              Now, the problem with the sort command is that it sorts elements in different line and can't sort elements in the same line. So, the workaround is to transform the array into an element per line and sort with sort and put them in an array which is actually sorted, that is,



              B=(`echo ${A[@]} | tr " " "n" | sort`)


              Now, B is the sorted array. Here, tr transforms space into a newline






              share|improve this answer




























                1














                Lets take an array A as



                A=(h4 h5 h1 h2 h3)


                Now, the problem with the sort command is that it sorts elements in different line and can't sort elements in the same line. So, the workaround is to transform the array into an element per line and sort with sort and put them in an array which is actually sorted, that is,



                B=(`echo ${A[@]} | tr " " "n" | sort`)


                Now, B is the sorted array. Here, tr transforms space into a newline






                share|improve this answer


























                  1












                  1








                  1






                  Lets take an array A as



                  A=(h4 h5 h1 h2 h3)


                  Now, the problem with the sort command is that it sorts elements in different line and can't sort elements in the same line. So, the workaround is to transform the array into an element per line and sort with sort and put them in an array which is actually sorted, that is,



                  B=(`echo ${A[@]} | tr " " "n" | sort`)


                  Now, B is the sorted array. Here, tr transforms space into a newline






                  share|improve this answer














                  Lets take an array A as



                  A=(h4 h5 h1 h2 h3)


                  Now, the problem with the sort command is that it sorts elements in different line and can't sort elements in the same line. So, the workaround is to transform the array into an element per line and sort with sort and put them in an array which is actually sorted, that is,



                  B=(`echo ${A[@]} | tr " " "n" | sort`)


                  Now, B is the sorted array. Here, tr transforms space into a newline







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 14 hours ago

























                  answered 14 hours ago









                  Ritajit Kundu

                  557




                  557






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • 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%2funix.stackexchange.com%2fquestions%2f490992%2fhow-do-i-sort-an-array-in-bash%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

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      Alexandru Averescu

                      Trompette piccolo