Simple array of strings sorting

E

Ed

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
 
W

William James

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

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"]
 
A

Andrew Stewart

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?

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

arr.sort_by { |x| x.split('-').last.to_i }

In IRB:
test-14 )
=> ["test-3", "test-31", "test-11", "test-12", "test-13", "test-32",
"test-10", "test-14"]=> ["test-10", "test-11", "test-12", "test-13", "test-14", "test-3",
"test-31", "test-32"]=> ["test-3", "test-10", "test-11", "test-12", "test-13", "test-14",
"test-31", "test-32"]

Regards,
Andy Stewart
 
E

Ed

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

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"]

Thanks for the response.
arr.split.sort_by{ |s| s[/\d+/].to_i } produces this error message:
"can't convert Regexp into Integer"
 
E

Ed

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?

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

arr.sort_by { |x| x.split('-').last.to_i }

In IRB:
test-14 )
=> ["test-3", "test-31", "test-11", "test-12", "test-13", "test-32",
"test-10", "test-14"]=> ["test-10", "test-11", "test-12", "test-13", "test-14", "test-3",
"test-31", "test-32"]=> ["test-3", "test-10", "test-11", "test-12", "test-13", "test-14",
"test-31", "test-32"]

Regards,
Andy Stewart

-------http://airbladesoftware.com

Works perfectly. Thanks a lot.
 
T

Todd Benson

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

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"]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top