Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Sorting numbers as strings

Reply
Thread Tools

Sorting numbers as strings

 
 
Jack Bauer
Guest
Posts: n/a
 
      05-19-2009
You guys are great. I went with Bob's (pick one, hah) since it ended up
being moderately faster than the method I was originally using.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Johan Holmberg
Guest
Posts: n/a
 
      05-19-2009
On Tue, May 19, 2009 at 5:24 PM, Jack Bauer <(E-Mail Removed)> wrote:
> You guys are great. I went with Bob's (pick one, hah) since it ended up
> being moderately faster than the method I was originally using.
> --


Here is a variant similar to the other solutions you already got:

puts arr.sort_by {|s| s.split(/(\d+)/).each_with_index.map {|x,i|
i.odd? ? x.to_i : x }}

Works with Ruby 1.9.

/Johan Holmberg

 
Reply With Quote
 
 
 
 
Rob Biedenharn
Guest
Posts: n/a
 
      05-20-2009
On May 19, 2009, at 5:51 PM, Johan Holmberg wrote:
> On Tue, May 19, 2009 at 5:24 PM, Jack Bauer
> <(E-Mail Removed)> wrote:
>> You guys are great. I went with Bob's (pick one, hah) since it
>> ended up
>> being moderately faster than the method I was originally using.
>> --

>
> Here is a variant similar to the other solutions you already got:
>
> puts arr.sort_by {|s| s.split(/(\d+)/).each_with_index.map {|x,i|
> i.odd? ? x.to_i : x }}
>
> Works with Ruby 1.9.
>
> /Johan Holmberg



Are you trying to say that you believe some part of a solution that
Jack already accepted won't work in Ruby 1.9? (Because they should
work fine.)

-Rob

Rob Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
Johan Holmberg
Guest
Posts: n/a
 
      05-20-2009
On Wed, May 20, 2009 at 6:24 AM, Rob Biedenharn
<(E-Mail Removed)> wrote:
> On May 19, 2009, at 5:51 PM, Johan Holmberg wrote:
>>
>> On Tue, May 19, 2009 at 5:24 PM, Jack Bauer <(E-Mail Removed)>
>> wrote:
>>>
>>> You guys are great. I went with Bob's (pick one, hah) since it ended up
>>> being moderately faster than the method I was originally using.
>>> --

>>
>> Here is a variant similar to the other solutions you already got:
>>
>> =A0 puts arr.sort_by {|s| s.split(/(\d+)/).each_with_index.map {|x,i|
>> i.odd? ? x.to_i : x }}
>>
>> Works with Ruby 1.9.
>>
>> /Johan Holmberg

>
>
> Are you trying to say that you believe some part of a solution that Jack
> already accepted won't work in Ruby 1.9? (Because they should work fine.)
>
> -Rob
>


No, not at all. I tried to say that I believed *my* variant only
worked in 1.9 (and maybe 1.8.7). (I should have said "Only works with
Ruby 1.9").

Sorry for the misunderstanding.

/Johan Holmberg

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: sorting a list numbers stored as strings Delaney, Timothy (Tim) Python 4 09-25-2007 06:36 PM
sorting a list numbers stored as strings aine_canby@yahoo.com Python 6 09-25-2007 05:31 AM
Numbers to strings to numbers again one man army Javascript 6 12-30-2005 07:05 AM
Sorting of numbers or strings. Mars C Programming 3 03-02-2005 06:13 PM
Sorting strings with characters and numbers Carsten Zerbst Java 6 08-14-2003 08:36 PM



Advertisments