title: Custom sorting array by groups with Ruby
body: <p>Please your help. I have the input_array with 3 groups of values. </p>
  
  <p>The group order I need is:</p>
  
  <pre><code>1st group: Pou
  2nd group: Apl
  3rd group: Gab
  </code></pre>
  
  <p>This order is already done in input_array, but I want to sort ascending each group.</p>
  
  <p>The script I have so far produces the output given in central column of table below (current output). </p>
  
  <pre><code>input_arr = ["Pou-12","Pou-7","Pou-4","Pou-8","Pou-9","Pou-11","Pou-10","Pou-3","Pou-2","Pou-1","Pou-6","Pou-5","Apl","Gab-3","Gab-5","Gab-4","Gab-1","Gab-2"] 
  
  b=input_arr.sort_by do |s|
    if s =~ /^\d+$/
        [2, $&amp;.to_i]
    else
        [1, s]
    end
  end
  
  puts b
  </code></pre>
  
  <p>and the desired sorted output array is shown below too.</p>
  
  <hr>
  
  <pre><code> input array  | current output | desired output array
  </code></pre>
  
  <hr>
  
  <pre><code>    Pou-12    |     Apl        |      Pou-1
      Pou-7     |     Gab-1      |      Pou-2
      Pou-4     |     Gab-2      |      Pou-3
      Pou-8     |     Gab-3      |      Pou-4
      Pou-9     |     Gab-4      |      Pou-5
      Pou-11    |     Gab-5      |      Pou-6
      Pou-10    |     Pou-1      |      Pou-7
      Pou-3     |     Pou-10     |      Pou-8
      Pou-2     |     Pou-11     |      Pou-9
      Pou-1     |     Pou-12     |      Pou-10
      Pou-6     |     Pou-2      |      Pou-11
      Pou-5     |     Pou-3      |      Pou-12
      Apl       |     Pou-4      |      Apl
      Gab-3     |     Pou-5      |      Gab-1
      Gab-5     |     Pou-6      |      Gab-2
      Gab-4     |     Pou-7      |      Gab-3
      Gab-1     |     Pou-8      |      Gab-4
      Gab-2     |     Pou-9      |      Gab-5
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Cary's solution with array instead of hash.</p>
  
  <pre><code>a = ["Pou-7","Pou-4","Gab-4","Pou-8","Pou-9","Pou-11","Pou-10","Pou-3","Pou-2","Pou-1","Pou-6","Pou-5","Apl","Gab-3","Gab-5","Gab-1","Pou-12","Gab-2"]
  order = [ "Pou", "Apl", "Gab" ]
  
  a.map{ |s| head,tail = s.split("-"); [order.index(head), tail.to_i, s]}.sort.map(&amp;:last)
  
  =&gt; ["Pou-1", "Pou-2", "Pou-3", "Pou-4", "Pou-5", "Pou-6", "Pou-7", "Pou-8", "Pou-9", 
  "Pou-10", "Pou-11", "Pou-12", "Apl", "Gab-1", "Gab-2", "Gab-3", "Gab-4", "Gab-5"]
  </code></pre>