From: Alexey Zilber Date: 2009-04-07T12:29:54+09:00 Subject: Re: how to sort a multi-elements array Hi, Was going through these older posts and have a similar question. I have a nested array that contains a date and a filename. I'm trying to sort by file date. The array is in the form: SortedFiles = [ [200904031109, "filea.txt"], [200904031337, "fileb.txt"], [200904031110, "filec.xt"] ] What I was trying to do was: SortedFiles.sort { |[[a,b],[x,y]] a < x | } That, unfortunately, did not work. I'm using Ruby 1.8.5 btw. Can anyone help out? Thanks, Alex Stefano Crocco wrote: > On Saturday 31 May 2008, Erwin wrote: >> 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 -- Posted via http://www.ruby-forum.com/.