From: Rick DeNatale Date: 2007-03-20T06:36:34+09:00 Subject: Re: Sorting Dates and Times in an array On 3/19/07, Paul wrote: > On Mar 19, 2:16 pm, "Rick DeNatale" wrote: > > On 3/19/07, Rob Biedenharn wrote: > > > > > The use of .sort_by causes the keys to be processed once and is > > > better for large Arrays than .sort (and if you need .sort!, just do > > > @date_array = @date_array.sort_by {...} instead). > > > > Not exactly the same if you have more than one reference to the Array. > > A more general equivalent to sort! would be: > > > > @date_array.replace(@date_array.sort_by {...}) > > > > Okay, I've tried to understand this but I'm not quite getting it yet. > Many of the replies posted here integrate the 'print' and the sorting > function, and while normally I'd applaud the efficiency that's not > what I need. I need to replace the original contents of the array > with the sorted contents. Printing afterwards just let's me confirm > that the sorting worked as expected. (By combining the two steps, I'm > only getting partial solutions.) Well that's what Rob and I were talking about. You've got two alternatives here. If you use Rob's suggestion: @date_array = @date_array.sort_by {...} You are changing the @date_array VARIABLE to refer to the new sorted array. This is okay as long as you don't have other variables which refer to the old array, and which you want to now refer to the new value. For a shorter example: a = [1,2,3] b = a a = a.reverse p a => [3, 2, 1] p b => [1, 2, 3] BUT a = [1, 2, 3] b = a a.reverse! # or a.replace(a.reverse) p a => [3, 2, 1] p b => [3, 2, 1] Since a and b still both refer to the same object > > I've tried using 'sort_by' but I'm not sure how to apply it to multi- > dimensional arrays. What would be the best way to replace the > contents if the data array is really large? Well, your example really is a multi-dimensional array, actually there's really no such thing in Ruby (there are add-ons line NArray but that's another story). You've got nested arrays. > Should I replace my other sort lines with something other? I noticed > that I cannot just change the 'sort' to 'sort_by' in the following > line: > > @date_array.sort! { |a,b| b[ 7 ] <=> a[ 7 ] } sort takes an optional block with two arguments to be compared, it should return -1 if the first argument is less than the first , 0 if they are equal and +1 if the first is greater than the second. This will get invoked many times while sorting a large array. So it can get expensive if the block takes any significant time. sort_by on the other hand takes a block which takes one argument which is an element in the collection to be sorted. This block returns an object which is used as a sort key for each element in the collection. I tried to give an example using sort_by before I read your requirement to have the sort descending by date and ascending by time. To use sort_by you need to come up with an object which represents the date and time and sorts that way. I didn't want to work that hard. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/