[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Sorting help

Binh Ly

10/12/2008 11:24:00 PM

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-....

18 Answers

Ron Askew

10/12/2008 11:48:00 PM

0

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



Matthew Moss

10/12/2008 11:52:00 PM

0

> 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 } }



Sebastian Hungerecker

10/12/2008 11:54:00 PM

0

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

Sebastian Hungerecker

10/12/2008 11:57:00 PM

0

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

Michael W. Ryder

10/13/2008 12:03:00 AM

0

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

Peña, Botp

10/13/2008 3:43:00 AM

0

RnJvbTogQmluaCBMeSBbbWFpbHRvOmJpbmhAcGlnYmFieS5uZXRdIA0KIyBHaXZlbiBhbiBhcnJh
eSB3aXRoIHZhbHVlcyBsaWtlIHRoaXMgWyIxMC4xIiwgIjcuNCIsICIxMC45IiwgIjEwLjExIiwN
CiMgIjEwLjEwIl0NCiMgd2hlbiBJIHNvcnQgaXQgaSBnZXQgc29tZXRoaW5nIGxpa2UgWyIxMC4x
IiwgIjEwLjEwIiwgIjEwLjExIiwgIjEwLjkiLCAiNy40Il0NCiMgV2hhdCBpcyB0aGUgYmVzdCB3
YXkgdG8gc29ydCBzdWNoIGFuIGFycmF5IG9mIHN0cmluZ3M/DQojIFdoYXQgaWYgdGhlIHN0cmlu
ZyB3YXMgZXh0ZW5kZWQgdG8gIjEwLjEuMjUiLg0KDQpvdGhlcnMgaGF2ZSBzaG93biBzcGxpdCtt
YXAgDQoNCnlvdSBjYW4gYWxzbyBkbyBpdCB3IHRoZSBodW1ibGUgc2NhbmYgKHllcywgbGlrZSBj
J3Mgc2NhbmYpDQoNCmVnLA0KDQphLnNvcnRfYnl7fHh8IHguc2NhbmYgIiVkLiVkIn0NCiM9PiBb
IjcuNCIsICIxMC4xIiwgIjEwLjkiLCAiMTAuMTAiLCAiMTAuMTEiXQ0KDQo=

Erik Veenstra

10/13/2008 12:08:00 PM

0


What about this one?:

module Enumerable
def sort_number_word_other
sort_by do |x|
x.to_s.scan(/\d+|\w+|[^\d\w]+/).collect do |s|
[s.to_i, s]
end
end
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.

Robert Klemme

10/13/2008 12:20:00 PM

0

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.

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

--
remember.guy do |as, often| as.you_can - without end

Joe Wölfel

10/13/2008 4:21:00 PM

0

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?

Robert Klemme

10/13/2008 5:34:00 PM

0

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

robert