On Sunday 30 August 2009, Paul wrote:
> |Hi there, I have an array of arrays that I want to sort by multiple
> |elements.
> |
> |Sample data in the array looks like: [ [ id, date, num, name ], [ id,
> |date, num, name ], ... ]
> |
> |I need to sort by : (1) name, (2) date, and (3) id.
> |
> |I can sort by any one element in the array no problem using something
> |
> |like :
> |> summary_data.sort! { |a,b| a[ 3 ] <=> b[ 3 ] }
> |
> |Unfortunately, I can't do this line more than once because it blows
> |away any previous sorting.
> |
> |I found a few pages on the internet that describe how to "sort an
> |array of Ruby objects by multiple class fields", however, I don't know
> |how to create a "class field".  I'm looking at an array, not a class.
> |
> |Is there a way to do this or do I need to convert my data to something
> |else so I can do what I need?
> |
> |Please help.
> |
> |Thanks.
> |

This should do what you want. It first compares the names then, if they're 
equal, it compares the dates. If the dates are also equal it compares the ids.

summary_data.sort! do |a, b|
 res = a[3] <=> b[3]
 res = a[1] <=> b[1] if res == 0
 res = a[0] <=> b[0] if res == 0
 res
end

I hope this helps

Stefano