From: Chuck Remes Date: 2009-08-31T04:38:14+09:00 Subject: Re: Sorting an array by multiple elements? On Aug 30, 2009, at 2:05 PM, Paul Carvalho wrote: > Got it! > >> The sort with the sample data above works as I expect, but for some >> reason it still isn't sorting correctly with my real data. I'll keep >> looking at it. I must be missing something else, although I can't >> think what it might be right now. >> > > The 'name' element turned out to be an array. (Doh!) I flattened the > data and the sort_by now works correctly, as expected. > > Thanks again for all your help. Be advised that sort_by sorts in ascending order by default. If you want to sort descending, things get a little complicated. For example, you can sort descending by putting a minus (-) sign in front of your Fixnum variables. That works nicely. Now try to do that with a string. Kaboom. Try this code to make it (mostly) work. # Added to the String class so in Enumerable#sort_by we can # use the #- to reverse the sort order from ascending to descending # Not perfect; see ruby-talk ML 20090320 where it shows that it doesn't # always work correctly class String def -@ self.gsub(/./) {|s| (255 - s[0]).chr } end end cr