raja / cs.indiana.edu (Raja S.) writes:

> One would expect that sending the message #sort to a string would
> alphabetize it but:
> 
>       'raja'.sort   #-> ['raja']
> 
>       rather than   #-> 'aajr'
> 
> Is this the desired behavior?

The String class is Enumerable, which means that #sort uses #each to
get the elements to sort.

String#each by default splits its receiver on the current record
separator, which is newline by default. So

  "larry\nmoe\ncurly".sort		# => ["curly", "larry\n", "moe\n"]

If you split explicitly, you can do fancier things:

  "raja".split(//).sort		# => ["a", "a", "j", "r"]


> Also, there doesn't seem to be a builtin String#sort!.  Something
> that the user can't write.  Seems like a useful addition.

That's because the sort behavior is imported from Enumerable. I'm not
sure that String#sort! would be that useful, though.


Regards


Dace