From: Su Zhang Date: 2011-07-02T00:44:27+09:00 Subject: Re: How to order Structs based on two fields On 7/1/2011 7:30 AM, I単aki Baz Castillo wrote: > I'll do some benchmarks and select the fastest one. The efficiency depends on how expensive it is to compute the key of the elements. If the computation is non-trivial, then Array#sort_by would be much faster because it caches the keys before the sort so that no unnecessary computation is done (i.e., the key of each element is computed exactly once). If, like it is in your case, the computation only involves several Fixnum comparisons, Array#sort_by will most likely be slower as it needs to allocate (cache) the key set prior to the actual sorting. For example, there should be a difference if the accessors themselves are expensive. Something like: def a sleep 0.01 @a end As a side note, this idiom used by Array#sort_by is known as the Schwartzian transform in the Perl world.