From: Ed Date: 2008-02-22T03:20:01+09:00 Subject: Re: Simple array of strings sorting On Feb 21, 1:06 pm, William James wrote: > On Feb 21, 11:54 am, Ed wrote: > > > > > 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"