Re: Sorting help

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@xxxxxxxxxxx [mailto:binh@xxxxxxxxxxx] 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
.

Relevant Pages

• Re: Oh look, another language (ceylon)
... Addition of numeric types is well defined in maths: ... Addition of strings requires interpreting the ... In the case of numerics the rules are widely ... Just because you can't define a sensible meaning for str * str doesn't ...
(comp.lang.python)
• Re: Evil type coercion
... > Try reading the original posts Russ, ... containing strings and numerics are compared, ... actually you were the unwitting victim of a rule in which no type coercion ...
(microsoft.public.vb.syntax)
• Re: A nongeneric bounded string array type (in database code)
... > and all numerics must be marshalled into and out of strings, ... Of course, I don't use strings for numeric data, and when I use ... Quality is scientific reality. ... -- from Zen and the Art of Motorcycle ...