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.
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.
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.
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.
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?
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.
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.
avoid doing it in the future.
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.
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.