From: RichardOnRails Date: 2009-03-18T23:22:25+09:00 Subject: Re: Complex sort of matrix possible, e.g. like Excel? On Mar 18, 6:16 am, Robert Klemme wrote: > 2009/3/18 Christopher Dicely : > > > Enumerable#sort lets you do this fairly easily with just a pure array > > of arrays, e.g., to sort the array-of-arrays "arr" by the first column > > ascending and the second descending: > > > arr.sort {|a,b| [a[0]<=>b[0], b[1]<=>a[1]].find {|x| x!=0} || 0} > > Alternatively with less intermediate Arrays and less comparison operations. > > arr.sort do |a,b| >  c = a[0]<=>b[0] >  c == 0 ? b[1]<=>a[1] : c > end > > You can as well do > > arr.sort_by {|a,b| [a[0]<=>b[0], b[1]<=>a[1]]} > > Cheers > > robert > > -- > remember.guy do |as, often| as.you_can - without end Hi Robert, As usual, you've got the perfect answer. Thank you very much. Best wishes, Richard