From: Yukihiro Matsumoto Date: 2005-10-19T22:59:11+09:00 Subject: Re: Array.sort when it's items are String inheritors with redefined <=> works like if not redefined Hi, In message "Re: Array.sort when it's items are String inheritors with redefined <=> works like if not redefined" on Wed, 19 Oct 2005 22:28:29 +0900, MiG writes: |Hello, | I want to have a string which, if in array, will be sorted like numbers. |I wrote this: | |----------------------------------------------------------------------------------------- | |class String2 < String | | def <=> str2 | self.to_i <=> str2.to_i | end | |end | |a = [ String2.new('1'), String2.new('10'), String2.new('5') ] | |puts a.sort.join(',') Why not use sort_by, much simpler solution? a = ['1', '10', '5'] puts a.sort_by{|x|x.to_u}.join(',') matz.