From: Paul Date: 2007-03-20T06:05:07+09:00 Subject: Re: Sorting Dates and Times in an array On Mar 19, 4:43 pm, "Phrogz" wrote: > > a) Ruby doesn't have multi-dimensional arrays in the core. Unless > you're using something like NArray, I suspect you're thinking of > arrays of arrays. In this case, your problem is 'just coding' - if you > want to sort an array of arrays, you can sort the primary axis and > then the subsequent arrays. Or you can choose to transpose the array > and sort along some other array. Maybe I am thinking of that.. I don't know that terminology. I understand a multi-dimensional array, but haven't heard of an "array of arrays" before. They sound the same to me. I'll go with your phrase. =) > > b) Replace the contents? Perhaps you really want map! to convert the > contents of the array in-place, and then to sort_by{} to twiddle the > bits. > > Or, use less memory and more CPU by using sort!. (It's generally easy > in computer programming to trade memory for calculation speed, in both > directions.) Okay, so I don't know what the difference is between these two: > @data.sort! { |a,b| b[x].to_f <=> a[x].to_f } # i.e. descending numeric sort and > @data.replace( @data.sort { |a,b| b[x].to_f <=> a[x].to_f } ) # i.e. descending numeric sort Does the former use more CPU and less memory, and the latter the opposite? When I checked the Programming Ruby reference it says that Array#map! is a 'synonym for Array#collect!' and when I check Array#collect! I don't think it's what I want in this sort function. Or if it is, I don't know how to write the code for it. And until today I had never seen 'sort_by' so I'm still trying to figure out that function too.