From: Sean O'Halpin Date: 2005-10-19T23:31:45+09:00 Subject: Re: Array.sort when it's items are String inheritors with redefined <=> works like if not redefined On 10/19/05, Ara.T.Howard wrote: > this avoids calling to_i, to_f, or whatever multiple times on the same object, > which will occur if you use either a spacship (<=>) operator or sort_by > approach. I thought sort_by was a packaged form of the Schwartzian transform, i.e. class A def to_i puts "in A.to_i" 3 end end p [1, 2, 3, 4, 5, A.new].sort_by {|x| x.to_i } # is equivalent to p [1, 2, 3, 4, 5, A.new].map{|x| [x.to_i, x]}.sort{|y, z| y[0] <=> z[0]}.map{|x| x[1]} __END__ in A.to_i [1, 2, 3, #, 4, 5] in A.to_i [1, 2, 3, #, 4, 5] Regards, Sean