Wilson Bilkovich
10/13/2006 11:11:00 PM
On 10/13/06, Dominic Son <dominicson@gmail.com> wrote:
> But how to do apply this to having 3 elements in the value of an array?
>
> container.sort_by { |x,y,z| *lost here*
> puts x <br>
> puts y <br>
> puts z <br>
> }
Having more entries in the array doesn't change the number of
arguments to the block.
a = []
a << [1,2,3]
a << [4,5,6]
a << [7,8,9]
a.sort_by do |element|
[element[0], element[1], element[2]]
end
If you return an Array from sort_by's block, it will sort on the
entries of the array in order.
In this trivial case, since you're sorting by all three, you could
just write the above as:
a.sort_by {|e| e}
(since 'e' is already an Array in the proper order)