# Sorting help

**URL:** https://rubytalk.org/t/sorting-help/49600
**Category:** ruby-talk
**Created:** [12 October 2008 23:24 UTC](https://rubytalk.org/t/sorting-help/49600 "2008-10-12T23:24:19Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://rubytalk.org/u/Binh_Ly)
#### Post date: [12 October 2008 23:24 UTC](https://rubytalk.org/t/sorting-help/49600/1 "2008-10-12T23:24:19Z")

</div>

Hello All,

I would like to request some helping with a sorting problem I have.

Given an array with values like this ["10.1", "7.4", "10.9", "10.11",  
"10.10"]

when I sort it i get something like ["10.1", "10.10", "10.11", "10.9",  
"7.4"]

What is the best way to sort such an array of strings?

What if the string was extended to "10.1.25".

Any advice would be greatly appreciated.

Thank you.

Binh

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Ron\_Askew](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@Ron\_Askew](https://rubytalk.org/u/Ron_Askew)
#### Post date: [12 October 2008 23:47 UTC](https://rubytalk.org/t/sorting-help/49600/2 "2008-10-12T23:47:52Z")

</div>

I the values are actually numeric, they should be stored as numerics,  
manipulated as numerics, and sorted as numerics.

So... the question is: WHY ARE THEY STRINGS?

> **···**
>
> -----Original Message-----  
> From: [binh@pigbaby.net](mailto:binh@pigbaby.net) [[mailto:binh@pigbaby.net](mailto:binh@pigbaby.net)]  
> Sent: Sunday, October 12, 2008 7:24 PM  
> To: ruby-talk ML  
> Subject: Sorting help
> 
> Hello All,
> 
> I would like to request some helping with a sorting problem I have.
> 
> Given an array with values like this ["10.1", "7.4", "10.9", "10.11",  
> "10.10"]
> 
> when I sort it i get something like ["10.1", "10.10", "10.11", "10.9",  
> "7.4"]
> 
> What is the best way to sort such an array of strings?
> 
> What if the string was extended to "10.1.25".
> 
> Any advice would be greatly appreciated.
> 
> Thank you.
> 
> Binh  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Matthew\_Moss](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@Matthew\_Moss](https://rubytalk.org/u/Matthew_Moss)
#### Post date: [12 October 2008 23:52 UTC](https://rubytalk.org/t/sorting-help/49600/3 "2008-10-12T23:52:27Z")

</div>

> I would like to request some helping with a sorting problem I have.
> 
> Given an array with values like this ["10.1", "7.4", "10.9", "10.11",  
> "10.10"]
> 
> when I sort it i get something like ["10.1", "10.10", "10.11", "10.9",  
> "7.4"]
> 
> What is the best way to sort such an array of strings?
> 
> What if the string was extended to "10.1.25".

I might do something like this:

arr.sort\_by { |str| str.split('.').map { |x| x.to\_i } }

---

<div class="post-metadata">

### Author: ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)
#### Post date: [12 October 2008 23:56 UTC](https://rubytalk.org/t/sorting-help/49600/4 "2008-10-12T23:56:38Z")

</div>

Binh Ly wrote:

> What is the best way to sort such an array of strings?

Having already given one (hopefully) helpful answer, I can't help that the way  
you phrased your question (or more precisely the way you didn't specify what  
you actually wanted), I was a little tempted to answer:  
\*That\* is the best way to sort an array of strings.

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

### Author: ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)
#### Post date: [13 October 2008 03:43 UTC](https://rubytalk.org/t/sorting-help/49600/5 "2008-10-13T03:43:26Z")

</div>

# Given an array with values like this ["10.1", "7.4", "10.9", "10.11",  
# "10.10"]  
# when I sort it i get something like ["10.1", "10.10", "10.11", "10.9", "7.4"]  
# What is the best way to sort such an array of strings?  
# What if the string was extended to "10.1.25".

others have shown split+map

you can also do it w the humble scanf (yes, like c's scanf)

eg,

a.sort\_by{|x| x.scanf "%d.%d"}  
#=\> ["7.4", "10.1", "10.9", "10.10", "10.11"]

> **···**
>
> From: Binh Ly [[mailto:binh@pigbaby.net](mailto:binh@pigbaby.net)]

---

<div class="post-metadata">

### Author: ![Erik\_Veenstra1](https://avatars.discourse-cdn.com/v4/letter/e/f04885/32.png) [@Erik\_Veenstra1](https://rubytalk.org/u/Erik_Veenstra1)
#### Post date: [13 October 2008 12:08 UTC](https://rubytalk.org/t/sorting-help/49600/6 "2008-10-13T12:08:15Z")

</div>

What about this one?:

module Enumerable  
&nbsp;&nbsp;&nbsp;def sort\_number\_word\_other  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort\_by do |x|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.to\_s.scan(/\d+|\w+|[^\d\w]+/).collect do |s|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[s.to\_i, s]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

It handles comparisons of strings like "ruby-1.8.7.tar.gz" pretty  
well. It even knows how to sort [3, "2.1"] in "a way".

> What is the best way to sort such an array of strings?

"best way"? Depends on the problem domain. There's no generic answer.

gegroet,  
Erik V.

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [13 October 2008 12:19 UTC](https://rubytalk.org/t/sorting-help/49600/7 "2008-10-13T12:19:59Z")

</div>

My advice so far would be to state what order you want to achieve.  
You just say what the output of a plain string sort is but not what  
you want it to be.

Cheers

robert

> **···**
>
> 2008/10/13 Binh Ly \<binh@pigbaby.net\>:
> 
> > Hello All,
> > 
> > I would like to request some helping with a sorting problem I have.
> > 
> > Given an array with values like this ["10.1", "7.4", "10.9", "10.11",  
> > "10.10"]
> > 
> > when I sort it i get something like ["10.1", "10.10", "10.11", "10.9",  
> > "7.4"]
> > 
> > What is the best way to sort such an array of strings?
> > 
> > What if the string was extended to "10.1.25".
> > 
> > Any advice would be greatly appreciated.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end

---

<div class="post-metadata">

### Author: ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)
#### Post date: [12 October 2008 23:54 UTC](https://rubytalk.org/t/sorting-help/49600/8 "2008-10-12T23:54:17Z")

</div>

Ron Askew wrote:

> I the values are actually numeric, they should be stored as numerics,  
> manipulated as numerics, and sorted as numerics.
> 
> So... the question is: WHY ARE THEY STRINGS?

PROBABLY BECAUSE THE OP REALIZED THAT 10.1.25 ISN'T REALLY A LEGAL NUMBER.  
And he either did not think of storing it as [10,1,25] or maybe he reads it  
from user input or from a file. Though of course the solution (to get  
numerical sorting) still is to get it into that form (split(".").map(&:to\_i))  
and then sort.

HTH,  
Sebastian

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

### Author: ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)
#### Post date: [13 October 2008 00:02 UTC](https://rubytalk.org/t/sorting-help/49600/9 "2008-10-13T00:02:20Z")

</div>

Ron Askew wrote:

> I the values are actually numeric, they should be stored as numerics,  
> manipulated as numerics, and sorted as numerics.
> 
> So... the question is: WHY ARE THEY STRINGS?

The data may be coming from another source that the OP has no control over. In the language I do most of my programming in, Business Basic, everything is stored as a string, so 12.35 is stored as "12.25" instead of it's binary representation.  
When I have this problem in Business Basic what I do is add leading zeros before storing the numbers in a file. The file system in Business Basic allows me to store records in a file and automatically sorts them by the key value. This means that you have to know in advance the size of the numbers.  
What I would do is store the strings as ["10.1", "07.4", "10.9", "10.11", "10.10"] and this should sort properly. In the second case I would store the number as "10.01.25". Now if the numbers are something like TCP addresses this method probably won't work.

> **···**
>
> > -----Original Message-----  
> > From: binh@pigbaby.net [mailto:binh@pigbaby.net] Sent: Sunday, October 12, 2008 7:24 PM  
> > To: ruby-talk ML  
> > Subject: Sorting help
> > 
> > Hello All,
> > 
> > I would like to request some helping with a sorting problem I have.
> > 
> > Given an array with values like this ["10.1", "7.4", "10.9", "10.11",  
> > "10.10"]
> > 
> > when I sort it i get something like ["10.1", "10.10", "10.11", "10.9",  
> > "7.4"]
> > 
> > What is the best way to sort such an array of strings?
> > 
> > What if the string was extended to "10.1.25".
> > 
> > Any advice would be greatly appreciated.
> > 
> > Thank you.
> > 
> > Binh

---

<div class="post-metadata">

### Author: ![Joe\_Wolfel](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joe\_Wolfel](https://rubytalk.org/u/Joe_Wolfel)
#### Post date: [13 October 2008 16:20 UTC](https://rubytalk.org/t/sorting-help/49600/10 "2008-10-13T16:20:49Z")

</div>

Hi All,

I'm looking for a Ruby audio API. Is there a good one already available or is anyone in the process of working on one? I'd like one that is easy to install and manage, such as a gem with no or automatic dependencies. Also, I'd like a liberal license, at least as liberal as LGPL or better yet BSD because I'd like to eventually give away the tools I'm working on with the fewest possible restrictions.

If I can't find one I'm considering writing one using something like PortAudio. Would that make sense? If I were to do that does anyone have any suggestions for how it should be done to make it work best with Ruby?

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [13 October 2008 17:32 UTC](https://rubytalk.org/t/sorting-help/49600/11 "2008-10-13T17:32:15Z")

</div>

Please do not hijack other threads. This is a completely different topic which deserves its own thread. Thank you!

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 13.10.2008 18:20, Joe Wölfel wrote:
> 
> > I'm looking for a Ruby audio API.

---

<div class="post-metadata">

### Author: ![Joe\_Wolfel](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joe\_Wolfel](https://rubytalk.org/u/Joe_Wolfel)
#### Post date: [13 October 2008 18:03 UTC](https://rubytalk.org/t/sorting-help/49600/12 "2008-10-13T18:03:20Z")

</div>

My apologies.

How exactly did I hijack another thread? Please let me know so I can avoid doing it in the future.

> **···**
>
> On 13 oct. 08, at 12:20, Joe Wölfel wrote:
> 
> > Hi All,
> > 
> > I'm looking for a Ruby audio API. Is there a good one already available or is anyone in the process of working on one? I'd like one that is easy to install and manage, such as a gem with no or automatic dependencies. Also, I'd like a liberal license, at least as liberal as LGPL or better yet BSD because I'd like to eventually give away the tools I'm working on with the fewest possible restrictions.
> > 
> > If I can't find one I'm considering writing one using something like PortAudio. Would that make sense? If I were to do that does anyone have any suggestions for how it should be done to make it work best with Ruby?

---

<div class="post-metadata">

### Author: ![Glen\_Holcomb](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Glen\_Holcomb](https://rubytalk.org/u/Glen_Holcomb)
#### Post date: [13 October 2008 17:50 UTC](https://rubytalk.org/t/sorting-help/49600/13 "2008-10-13T17:50:25Z")

</div>

Maybe it's just me but I don't see any other conversation here but the audio  
question.

Joe,

I haven't used anything like what you are describing but there are some Ruby  
Audio libraries:

[http://hans.fugal.net/src/ruby-audio/doc/](http://hans.fugal.net/src/ruby-audio/doc/)  
[http://raa.ruby-lang.org/cat.rhtml?category\_major=Library;category\_minor=Audio](http://raa.ruby-lang.org/cat.rhtml?category_major=Library;category_minor=Audio)

-Glen

> **···**
>
> On Mon, Oct 13, 2008 at 11:32 AM, Robert Klemme \<shortcutter@googlemail.com\>wrote:
> 
> > On 13.10.2008 18:20, Joe Wölfel wrote:
> > 
> > > I'm looking for a Ruby audio API.
> > 
> > Please do not hijack other threads. This is a completely different topic  
> > which deserves its own thread. Thank you!
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert
> 
> --  
> "Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can't hear a word you're saying."
> 
> -Greg Graffin (Bad Religion)

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [13 October 2008 18:47 UTC](https://rubytalk.org/t/sorting-help/49600/14 "2008-10-13T18:47:22Z")

</div>

> My apologies.

Accepted.

> How exactly did I hijack another thread? Please let me know so I can avoid doing it in the future.

I do not know how you did it, but in c.l.r (Usenet newsgroup) your topic references "Sorting Help". Maybe you just replied to a message on that thread.

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 13.10.2008 20:03, Joe Wölfel wrote:

---

<div class="post-metadata">

### Author: ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)
#### Post date: [13 October 2008 18:56 UTC](https://rubytalk.org/t/sorting-help/49600/15 "2008-10-13T18:56:39Z")

</div>

Right, if you reply to a message and replace its subject, the  
threading is still preserved from the original message. My guess is  
the OP did that by accident, and the problem can be solved by starting  
a new message rather than using reply and changing the subject.

It is only a good idea to change the subject within a thread to show a  
topic that has diverged from the original but is still of the same  
originating conversation. New conversations deserve new toplevel  
threads.

-greg

> **···**
>
> On Mon, Oct 13, 2008 at 2:47 PM, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On 13.10.2008 20:03, Joe Wölfel wrote:
> > 
> > > My apologies.
> > 
> > Accepted.
> > 
> > > How exactly did I hijack another thread? Please let me know so I can  
> > > avoid doing it in the future.
> > 
> > I do not know how you did it, but in c.l.r (Usenet newsgroup) your topic  
> > references "Sorting Help". Maybe you just replied to a message on that  
> > thread.
> 
> --  
> Technical Blaag at: [http://blog.majesticseacreature.com](http://blog.majesticseacreature.com) | Non-tech  
> stuff at: [http://metametta.blogspot.com](http://metametta.blogspot.com)

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [13 October 2008 18:57 UTC](https://rubytalk.org/t/sorting-help/49600/16 "2008-10-13T18:57:16Z")

</div>

Where is "here"? I am looking at Usenet at the moment.

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 13.10.2008 19:50, Glen Holcomb wrote:
> 
> > On Mon, Oct 13, 2008 at 11:32 AM, Robert Klemme \> \<shortcutter@googlemail.com\>wrote:
> > 
> > > On 13.10.2008 18:20, Joe Wölfel wrote:
> > > 
> > > > I'm looking for a Ruby audio API.
> > > 
> > > Please do not hijack other threads. This is a completely different topic  
> > > which deserves its own thread. Thank you!
> > 
> > Maybe it's just me but I don't see any other conversation here but the audio  
> > question.

---

<div class="post-metadata">

### Author: ![Joe\_Wolfel](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joe\_Wolfel](https://rubytalk.org/u/Joe_Wolfel)
#### Post date: [13 October 2008 19:04 UTC](https://rubytalk.org/t/sorting-help/49600/17 "2008-10-13T19:04:09Z")

</div>

> > > My apologies.
> > 
> > Accepted.
> > 
> > > How exactly did I hijack another thread? Please let me know so I can  
> > > avoid doing it in the future.
> > 
> > I do not know how you did it, but in c.l.r (Usenet newsgroup) your topic  
> > references "Sorting Help". Maybe you just replied to a message on that  
> > thread.
> 
> Right, if you reply to a message and replace its subject, the  
> threading is still preserved from the original message. My guess is  
> the OP did that by accident, and the problem can be solved by starting  
> a new message rather than using reply and changing the subject.
> 
> It is only a good idea to change the subject within a thread to show a  
> topic that has diverged from the original but is still of the same  
> originating conversation. New conversations deserve new toplevel  
> threads.
> 
> -greg

Possible. I can't remember what exactly I was doing. I'll repost in a fresh window. Again, I didn't mean to cause any trouble.

> **···**
>
> On 13 oct. 08, at 14:56, Gregory Brown wrote:
> 
> > On Mon, Oct 13, 2008 at 2:47 PM, Robert Klemme \> \<shortcutter@googlemail.com\> wrote:
> > 
> > > On 13.10.2008 20:03, Joe Wölfel wrote:
> 
> > --  
> > Technical Blaag at: [http://blog.majesticseacreature.com](http://blog.majesticseacreature.com) | Non-tech  
> > stuff at: [http://metametta.blogspot.com](http://metametta.blogspot.com)

---

<div class="post-metadata">

### Author: ![Glen\_Holcomb](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Glen\_Holcomb](https://rubytalk.org/u/Glen_Holcomb)
#### Post date: [13 October 2008 19:31 UTC](https://rubytalk.org/t/sorting-help/49600/18 "2008-10-13T19:31:16Z")

</div>

I was reading in my email, It had shown as a new topic in gmail.

> **···**
>
> On Mon, Oct 13, 2008 at 12:57 PM, Robert Klemme \<shortcutter@googlemail.com\>wrote:
> 
> > On 13.10.2008 19:50, Glen Holcomb wrote:
> > 
> > > On Mon, Oct 13, 2008 at 11:32 AM, Robert Klemme \>\> \<shortcutter@googlemail.com\>wrote:
> > > 
> > > On 13.10.2008 18:20, Joe Wölfel wrote:
> > > 
> > > > I'm looking for a Ruby audio API.
> > > > 
> > > > > Please do not hijack other threads. This is a completely different
> > > > 
> > > > topic  
> > > > which deserves its own thread. Thank you!
> > > > 
> > > > Maybe it's just me but I don't see any other conversation here but the
> > > 
> > > audio  
> > > question.
> > 
> > Where is "here"? I am looking at Usenet at the moment.
> > 
> > Cheers
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert
> 
> --  
> "Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can't hear a word you're saying."
> 
> -Greg Graffin (Bad Religion)

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [14 October 2008 07:02 UTC](https://rubytalk.org/t/sorting-help/49600/19 "2008-10-14T07:02:08Z")

</div>

Ah, this might be an explanation: I believe GMail's web frontend does  
not consider mails with different subjects to be in the same thread.  
That would explain, why you did not see this.

Cheers

robert

> **···**
>
> 2008/10/13 Glen Holcomb \<damnbigman@gmail.com\>:
> 
> > On Mon, Oct 13, 2008 at 12:57 PM, Robert Klemme \> \<shortcutter@googlemail.com\>wrote:
> > 
> > > On 13.10.2008 19:50, Glen Holcomb wrote:
> 
> > > > > Maybe it's just me but I don't see any other conversation here but the
> > > > 
> > > > audio  
> > > > question.
> 
> > > Where is "here"? I am looking at Usenet at the moment.
> 
> > I was reading in my email, It had shown as a new topic in gmail.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end
