From: Stefano Crocco Date: 2008-06-01T02:42:22+09:00 Subject: Re: how to sort a multi-elements array On Saturday 31 May 2008, Erwin wrote: > I know how to sort an array with 2 elements... that's the only example > I could fin on Array#sort... > but what if ... I need to sort an array like this one : > > anArray = [ [1, "joe", 4], [2, "arthur", 2], [5, "william", 3], [16, > "bob", 1], [20, "ernesto", 17], [27, "julia", 1], [28, "barbara", 2] ] > > sorting on third-element (highest ranking) , and second-element > (nickname) > > aSortedArray = [ [20, "ernesto", 17], [1, "joe", 4], 5, "william", 3], > [2, "arthur", 2], [28, "barbara", 2], [16, "bob", 1] ] > > any doc link where this is explained ? > > thanks a lot for your tip You can do this: aSortedArray = anArray.sort do |a, b| if a[2] == b[2] then a[1] <=> b[1] else a[2] <=> b[2] end end If the third element of the two subarrays are equal, then the block returns the result of the comparison of the second elements (using <=>), otherwise it returns the result of the comparison of the third elements, again using <=>. I hope this helps Stefano