From: Phrogz Date: 2007-03-20T07:10:12+09:00 Subject: Re: Sorting Dates and Times in an array On Mar 19, 3:02 pm, "Paul" wrote: [snip] > And until today I had never > seen 'sort_by' so I'm still trying to figure out that function too. Having recommended you read the documentation, let me try to explain it (since the documentation isn't as clear as I think I can do here): Say you have an array of 1,000 elements. When you sort them, the sorting algorithm might perform (on average) something like 7,000 comparisons to sort your items, but in some cases it might perform 1,000,000 comparisons. If every element is a number, that's about as good as you're going to get. Now imagine that every element is instead a person, and you want to sort them by something like their body mass index plus their age times the average weight of all their children. Something that takes a long time to calculate. If you write: my_array.sort{ |a,b| a.long_calc <=> b.long_calc } then you might have to perform that complex calculation 1,000,000 times to sort your array. Less than ideal. If instead you write: my_array.sort{ |person| person.long_calc } then Ruby will first loop through your items and run long_calc for each one, saving the values. Exactly 1,000 calls to long_calc. THEN it will use those values to sort your original array, able to quickly compare the saved result for each. That's the performance reason to use sort_by. I personally use it because it's much less typing. :)