How do I sort an array with letters and numbers combined in bash? [duplicate]












2
















This question already has an answer here:




  • How to create a function that can sort an array in bash?

    4 answers




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















marked as duplicate by roaima, Jeff Schaller, Volker Siegel, RalfFriedl, msp9011 Dec 27 '18 at 6:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • That's a string; do you have that, exactly, or an actual array?

    – Jeff Schaller
    Dec 26 '18 at 14:42











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

    – Mercyfon
    Dec 26 '18 at 14:56
















2
















This question already has an answer here:




  • How to create a function that can sort an array in bash?

    4 answers




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















marked as duplicate by roaima, Jeff Schaller, Volker Siegel, RalfFriedl, msp9011 Dec 27 '18 at 6:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • That's a string; do you have that, exactly, or an actual array?

    – Jeff Schaller
    Dec 26 '18 at 14:42











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

    – Mercyfon
    Dec 26 '18 at 14:56














2












2








2









This question already has an answer here:




  • How to create a function that can sort an array in bash?

    4 answers




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

















This question already has an answer here:




  • How to create a function that can sort an array in bash?

    4 answers




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.





This question already has an answer here:




  • How to create a function that can sort an array in bash?

    4 answers








bash sort array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 '18 at 13:20







Mercyfon

















asked Dec 26 '18 at 14:35









MercyfonMercyfon

205




205




marked as duplicate by roaima, Jeff Schaller, Volker Siegel, RalfFriedl, msp9011 Dec 27 '18 at 6:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by roaima, Jeff Schaller, Volker Siegel, RalfFriedl, msp9011 Dec 27 '18 at 6:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • That's a string; do you have that, exactly, or an actual array?

    – Jeff Schaller
    Dec 26 '18 at 14:42











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

    – Mercyfon
    Dec 26 '18 at 14:56



















  • That's a string; do you have that, exactly, or an actual array?

    – Jeff Schaller
    Dec 26 '18 at 14:42











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

    – Mercyfon
    Dec 26 '18 at 14:56

















That's a string; do you have that, exactly, or an actual array?

– Jeff Schaller
Dec 26 '18 at 14:42





That's a string; do you have that, exactly, or an actual array?

– Jeff Schaller
Dec 26 '18 at 14:42













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

– Mercyfon
Dec 26 '18 at 14:56





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

– Mercyfon
Dec 26 '18 at 14:56










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
      Dec 26 '18 at 22:22



















    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
































      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 Dec 26 '18 at 14:49









          msp9011msp9011

          4,07844064




          4,07844064

























              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
                Dec 26 '18 at 22:22
















              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
                Dec 26 '18 at 22:22














              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 Dec 26 '18 at 17:50









              RudiCRudiC

              4,2191312




              4,2191312













              • This would be better if echo was substituted for printf

                – D. Ben Knoble
                Dec 26 '18 at 22:22



















              • This would be better if echo was substituted for printf

                – D. Ben Knoble
                Dec 26 '18 at 22:22

















              This would be better if echo was substituted for printf

              – D. Ben Knoble
              Dec 26 '18 at 22:22





              This would be better if echo was substituted for printf

              – D. Ben Knoble
              Dec 26 '18 at 22:22











              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 Dec 26 '18 at 14:53

























                  answered Dec 26 '18 at 14:46









                  Ritajit KunduRitajit Kundu

                  857




                  857















                      Popular posts from this blog

                      Plaza Victoria

                      In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                      How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...