From: Brian Candler Date: 2003-05-19T16:21:26+09:00 Subject: Re: ruby-dev summary #20159-20200 On Mon, May 19, 2003 at 02:17:36PM +0900, Joel VanderWerf wrote: > >However Python behaves bit differently on numbers. > > I hope so... > > p [1, "1", 1.5, 2].sort_by {|v| [v.class.name, v] } > > # ==> [1, 2, 1.5, "1"] > > How about using class.name as a fallback, so that Floats and Integers > get sorted together? Surely if you have a mixed bag of objects, it should be your responsibility to define the ordering? i.e. if they are "like numbers" then p [2, 1, "1", 1.5].sort { |x,y| x.to_f <=> y.to_f } # ==> ["1", 1, 1.5, 2] and if they are "like strings" then use x.to_s <=> y.to_s If you don't do that, then I'd much prefer an exception to be raised (i.e. "you are trying to do something stupid") than have "sorted" arrays returned with a strange ordering just because 1 <=> "2" returns nil. And I certainly wouldn't want the automatic equivalence between numbers and strings-with-digits which Perl applies. Regards, Brian.