# Simple array of strings sorting

**URL:** <https://www.ruby-forum.com/t/simple-array-of-strings-sorting/129811>\
**Category:** Ruby\
**Created:** [February 21, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/simple-array-of-strings-sorting/129811 "2008-02-21T18:55:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 21, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/simple-array-of-strings-sorting/129811/1 "2008-02-21T18:55:45Z")

</div>

Say I have an array of strings that looks like this:  
test-3  
test-31  
test-11  
test-12  
test-13  
test-32  
test-10  
test-14

I want them sorted like so:  
test-3  
test-10  
test-11  
test-12  
test-13  
test-14  
test-31  
test-32

arr.sort doesn’t seem to work the way I need. Any help?  
Thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 21, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/simple-array-of-strings-sorting/129811/2 "2008-02-21T19:11:02Z")

</div>

On Feb 21, 11:54 am, Ed [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want them sorted like so:  
> Thanks  
> The sequences of numerals need to be treated as numbers,  
> not strings.

" test-3  
test-31  
test-11  
test-12  
test-13  
test-32  
test-10  
test-14 ".split.sort\_by{|s| s[/\d+/].to\_i}  
==\>[“test-3”, “test-10”, “test-11”, “test-12”, “test-13”,  
“test-14”, “test-31”, “test-32”]

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 21, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/simple-array-of-strings-sorting/129811/3 "2008-02-21T19:16:54Z")

</div>

On 21 Feb 2008, at 17:55, Ed wrote:

> I want them sorted like so:  
> test-3  
> test-10  
> test-11  
> test-12  
> test-13  
> test-14  
> test-31  
> test-32
> 
> arr.sort doesn’t seem to work the way I need. Any help?

It’s sorting lexicographically rather than numerically. Try this  
instead:

arr.sort\_by { |x| x.split(’-’).last.to\_i }

In IRB:

> > arr = %w( test-3 test-31 test-11 test-12 test-13 test-32 test-10  
> > test-14 )  
> > =\> [“test-3”, “test-31”, “test-11”, “test-12”, “test-13”, “test-32”,  
> > “test-10”, “test-14”]
> 
> > arr.sort  
> > =\> [“test-10”, “test-11”, “test-12”, “test-13”, “test-14”, “test-3”,  
> > “test-31”, “test-32”]
> 
> > arr.sort\_by { |x| x.split(’-’).last.to\_i }  
> > =\> [“test-3”, “test-10”, “test-11”, “test-12”, “test-13”, “test-14”,  
> > “test-31”, “test-32”]

Regards,  
Andy S.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 21, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/simple-array-of-strings-sorting/129811/4 "2008-02-21T19:21:00Z")

</div>

On Feb 21, 1:06 pm, William J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > test-32  
> > test-31  
> > test-11  
> > test-12  
> > test-13  
> > test-32  
> > test-10  
> > test-14 ".split.sort\_by{|s| s[/\d+/].to\_i}  
> > ==\>[“test-3”, “test-10”, “test-11”, “test-12”, “test-13”,  
> > “test-14”, “test-31”, “test-32”]

Thanks for the response.  
arr.split.sort\_by{ |s| s[/\d+/].to\_i } produces this error message:  
“can’t convert Regexp into Integer”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 21, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/simple-array-of-strings-sorting/129811/5 "2008-02-21T19:31:03Z")

</div>

On Feb 21, 1:14 pm, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > test-32  
> > test-31
> 
> Regards,  
> Andy S.
> 
> -------[http://airbladesoftware.com](http://airbladesoftware.com)

Works perfectly. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 21, 2008, 7:42pm UTC](https://www.ruby-forum.com/t/simple-array-of-strings-sorting/129811/6 "2008-02-21T19:42:49Z")

</div>

On Thu, Feb 21, 2008 at 12:20 PM, Ed [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > test-12  
> > > test-13  
> > > " test-3  
> > > Thanks for the response.  
> > > arr.split.sort\_by{ |s| s[/\d+/].to\_i } produces this error message:  
> > > “can’t convert Regexp into Integer”

William was splitting the string as an example, not an array. Try…

arr.sort\_by { |s| s[/\d+/].to\_i }

… if you trust your data (i.e. you don’t accidentally have numbers  
hanging out elsewhere, like test1-41)

Todd
