Martin DeMello wrote:
> pete boardman <pete.boardman / pobox.com> wrote:
>> On 10 Oct 2005, at 22:53, James Edward Gray II wrote:
>>
>>> arr = eval "{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4, 2.2}, {1.4, 2.2,
>>> 2.1, 4.2}}".tr("{}", "[]")
>>> arr.sort { |a, b| a.first <=> b.first }
>>
>> Thanks, James - simple, elegant, and nearly perfect! Is it easy to
>> sort the array of arrays by their second, third, and fourth entries
>> as well:
>
> That's actually the default behaviour, so you can just say arr.sort

Strictly speaking that's wrong: the code above and arr.sort are not
equivalent because arr.sort will evaluate all elements of embedded arrays
while the code above will only honor the first one:

>> a=[[1,2,3],[1,0,0]]
=> [[1, 2, 3], [1, 0, 0]]
>> a.sort
=> [[1, 0, 0], [1, 2, 3]]
>> a.sort_by {|x| x.first}
=> [[1, 2, 3], [1, 0, 0]]
>> a.sort {|x,y| x.first <=> y.first}
=> [[1, 2, 3], [1, 0, 0]]

Kind regards

    robert