From: Ryan Leavengood Date: 2005-10-20T00:48:03+09:00 Subject: Re: Array.sort when it's items are String inheritors with redefined <=> works like if not redefined I think you have a bug here, Ara: On 10/19/05, Ara.T.Howard wrote: > > harp:~ > cat a.rb > class Array > def sort_as! > map!{|elem| yield elem} > sort! > self > end > def sort_as > dup.sort! #??? > end How about: def sort_as(&block) dup.sort_as!(&block) end > end > > > a = %w( 1 10 5 ) > > p a.sort_as{|s| Integer s} > p a.sort_as{|s| Float s} > p a.sort_as{|s| s.reverse } > > a.sort_as!{|s| Integer s} > p a C:\_Ryan\ruby>ruby a.rb [1, 5, 10] [1.0, 5.0, 10.0] ["01", "1", "5"] [1, 5, 10] I'm not sure it is worth adding such a method, when a map{}.sort would do the same thing, and is more explicit. Plus we have sort_by to solve the OP's problem. Ryan