Q:

Iterate an array of hashes and output each iteration in a row

I have an array of hashes as in example:
[{:number=>2131, :owner=>"Mark"},
 {:number=>223, :owner=>"Mark"},
 {:number=>546, :owner=>"Mark"},
 {:number=>765454, :owner=>"Tom"},
 {:number=>845378, :owner=>"Jack"},
 {:number=>75, :owner=>"Jack"},
 {:number=>2342, :owner=>"Jack"}]

How can I output number values of the owners sorted line by line to get this:
# ["Jack", "Mark", "Tom"]
75 223 765454
2342 546 -
845378 2131 -

Each column with sorted numbers belongs to an owner, each row contains numbers from each owner.

A:

What about
a = [{:number=>2131, :owner=>"Mark"},
     {:number=>223, :owner=>"Mark"},
     {:number=>546, :owner=>"Mark"},
     {:number=>765454, :owner=>"Tom"},
     {:number=>845378, :owner=>"Jack"},
     {:number=>75, :owner=>"Jack"},
     {:number=>2342, :owner=>"Jack"}]

a1 = a.group_by { |h| h[:owner]  }
#=> {"Mark"=>[{:number=>2131, :owner=>"Mark"}, {:number=>223, :owner=>"Mark"}, {:number=>546, :owner=>"Mark"}], "Tom"=>[{:number=>765454, :owner=>"Tom"}], "Jack"=>[{:number=>845378, :owner=>"Jack"}, {:number=>75, :owner=>"Jack"}, {:number=>2342, :owner=>"Jack"}]}

a2 = a1.map { |k, v| [k, v.sort_by { |v| v[:number] } ] }
#=> [["Mark", [{:number=>223, :owner=>"Mark"}, {:number=>546, :owner=>"Mark"}, {:number=>2131, :owner=>"Mark"}]], ["Tom", [{:number=>765454, :owner=>"Tom"}]], ["Jack", [{:number=>75, :owner=>"Jack"}, {:number=>2342, :owner=>"Jack"}, {:number=>845378, :owner=>"Jack"}]]]

a3 = a2.sort_by { |(v0)| v0 }.to_h
#=> {"Jack"=>[{:number=>75, :owner=>"Jack"}, {:number=>2342, :owner=>"Jack"}, {:number=>845378, :owner=>"Jack"}], "Mark"=>[{:number=>223, :owner=>"Mark"}, {:number=>546, :owner=>"Mark"}, {:number=>2131, :owner=>"Mark"}], "Tom"=>[{:number=>765454, :owner=>"Tom"}]}

max_values_size = a3.values.max_by { |v| v.size }.size
#=> 3

a4 = max_values_size.times.map do |i|
  a3.keys.map do |k|
    a3[k][i] ? a3[k][i][:number] : '-'
  end
end
#=> [[75, 223, 765454], [2342, 546, "-"], [845378, 2131, "-"]]

a4.each { |v| puts v.join(' ') }
#=>
75 223 765454 
2342 546 - 
845378 2131 -

It should be quite self-explanatory, ask if unclear

