From: James Coglan Date: 2008-08-12T00:27:00+09:00 Subject: Re: Sort array by two attributes? (like sql "order by A, B") ------=_Part_44149_22422043.1218468561655 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline > > Can anyone think of a way to do the same thing with a ruby array? For > example, if the array holds objects that have attributes/methods > "lastname" & "firstname", to order the objects in a similar way to the > sql query? Just use Enumerable#sort: http://ruby-doc.org/core/classes/Enumerable.html#M003150 objects.sort do |a,b| comp = (a.lastname <=> b.lastname) comp.zero? ? (a.firstname <=> b.firstname) : comp end James http://blog.jcoglan.com http://github.com/jcoglan ------=_Part_44149_22422043.1218468561655--