From: cruiserdan Date: 2007-12-07T07:30:07+09:00 Subject: Re: sorting Array of accentuated Strings On Dec 6, 11:36 am, unbewusst.s...@weltanschauung.com.invalid (Une Bévue) wrote: > I've done a "self <=> anotherString" comparaison which works by itself : > > class String > def <=>( aString ) > [blahblahblah] > end > end > > however if i want to sort an Array of Strings, the Array is sorted as > usual within Ruby (put accentuated characters at the end) : > > a = [ "Être", "Fenêtre", "Etre" ] > > b = a.sort { | i, j | i <=> j } > puts "b = [ " + b.join(", ") + " ]\n" > # => b = [ Etre, Être, Fenêtre ] > > c = a.sort #<=>( aString ) NOT CALLED... > puts "c = [ " + c.join(", ") + " ]\n" > # => c = [ Etre, Fenêtre, Être ] > > Why, in the case of "c = a.sort", #<=>( aString ) isn't called ? > the comparaison within an Array could compare different kind of objects > like : > > a = [ 0, "a", 9, Time.new ] ??? > > -- > Une Bévue I believe that the C implementation Array#sort checks for a String argument and calls the C string comparison operator directly in that case. See: http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/array.c?view=markup under the code for sort_2. You might be interested in using some of the emerging Unicode support in Ruby. Ruby 2 will have it built-in and there are several libraries out there, although I don't have any experience using them. Regards, Dan Yoder http://dev.zeraweb.com/