From: Rick DeNatale Date: 2009-04-07T21:26:27+09:00 Subject: Re: how to sort a multi-elements array --001636164afd8168de0466f6220a Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Tue, Apr 7, 2009 at 12:31 AM, Christopher Dicely wrote: > On 4/6/09, Alexey Zilber wrote: > > 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? > > SortedFiles.sort_by {|date, fname| date} > or: > SortedFiles.sort {|record1,record2| record1[0]<=>record2[0]} > > Since you are sorting on the first element of each array you can just use SortedFiles.sort Array comparison with another array works by using the comparison of the first pair of elements which aren't equal, so [1,1] < [2,0] < [2,1] -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale --001636164afd8168de0466f6220a--