Michael W. Ryder
10/13/2008 12:03:00 AM
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