From: Paul Carvalho Date: 2009-08-31T02:25:05+09:00 Subject: Re: Sorting an array by multiple elements? On Aug 30, 11:41 am, "David A. Black" wrote: > > The nothing result seems strange. This: [snip] > >   data = [ [2, "2009-08-21", 2, "alfie"], >            [6, "2009-08-23", 3, "alfie"], >            [1, "2009-08-21", 4, "barny"], >            [3, "2009-08-23", 3, "alfie"], >            [5, "2009-08-24", 1, "cliff"], >            [4, "2009-08-21", 1, "cliff", "bob"] ] > >    data.sort_by {|id, date, num, name| [name, date, id] } > > gives me: > >         [2, "2009-08-21", 2, "alfie"] >         [3, "2009-08-23", 3, "alfie"] >         [6, "2009-08-23", 3, "alfie"] >         [1, "2009-08-21", 4, "barny"] >         [4, "2009-08-21", 1, "cliff", "bob"] >         [5, "2009-08-24", 1, "cliff"] > Thanks David, I think I know what the happened now. When I tried the other approach, I sorted in place using: data.sort! But when I used "data.sort_by" it might have sorted it but didn't save it back to the same array. "sort_by!" doesn't exist so I'll need to save the sorted data to another variable. 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. Sort the data, write it out to file. Should be straightforward. Cheers.