# Unintended consequence: string.sort

**URL:** <https://www.ruby-forum.com/t/unintended-consequence-string-sort/108637>\
**Category:** Ruby\
**Created:** [July 5, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/unintended-consequence-string-sort/108637 "2007-07-05T15:37:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [July 5, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/unintended-consequence-string-sort/108637/1 "2007-07-05T15:37:20Z")

</div>

irb(main):001:0\> “hello world”.sort  
=\> [“hello world”]

(Because Enumerable#sort calls to\_a, and String#to\_a = [string])

However this is inconsistent with String#each, and almost certainly  
never what the user wants. I’d suggest  
String.split($/).sort.join($/).

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [July 5, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/unintended-consequence-string-sort/108637/2 "2007-07-05T15:44:21Z")

</div>

Hi –

On Thu, 5 Jul 2007, Martin DeMello wrote:

> irb(main):001:0\> “hello world”.sort  
> =\> [“hello world”]
> 
> (Because Enumerable#sort calls to\_a, and String#to\_a = [string])
> 
> However this is inconsistent with String#each, and almost certainly  
> never what the user wants. I’d suggest  
> String.split($/).sort.join($/).

I don’t think it’s inconsistent with String#each:

> > “hello world”.each {|x| p x }  
> > “hello world”

If you have multiple lines it will sort that way:

> > “hello\nworld”.sort  
> > =\> [“hello\n”, “world”]

David

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [July 5, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/unintended-consequence-string-sort/108637/3 "2007-07-05T15:57:23Z")

</div>

This may help U?

irb(main):001:0\> “hello world”.split(/|/).sort  
=\> [" ", “d”, “e”, “h”, “l”, “l”, “l”, “o”, “o”, “r”, “w”]

2007/7/5, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [July 5, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/unintended-consequence-string-sort/108637/4 "2007-07-05T16:01:23Z")

</div>

On 7/5/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > However this is inconsistent with String#each, and almost certainly
> > 
> > > “hello\nworld”.sort  
> > > =\> [“hello\n”, “world”]

Just to continue, String#to\_a != [string] either.  
“a\nb\nc\n”.to\_a #=\> [“a\n”, “b\n”, “c\n”]

David

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [July 5, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/unintended-consequence-string-sort/108637/5 "2007-07-05T16:54:34Z")

</div>

On 7/5/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you have multiple lines it will sort that way:
> 
> > > “hello\nworld”.sort  
> > > =\> [“hello\n”, “world”]

Huh - I thought I tried that and it didn’t work. Would still be nicer  
for it to return a string (since it can), but I agree it’s consistent.

martin
