Re: Sorting help



From: Binh Ly [mailto:binh@xxxxxxxxxxx]
# 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"]



Relevant Pages

  • Re: Any suggestions?
    ... I don't have lame (some sort of MP3 decoder?) installed on my system, ... strings into the @num array. ... This converts the array into a string with spaces separating the array ... In the example, $num is "/8575". ...
    (comp.lang.perl.misc)
  • Re: Sorting Algorithm required!
    ... Here are string versions of merge and radix sort. ... an array containing the strings you want to sort. ...
    (microsoft.public.excel.programming)
  • Re: A simple sorting algorithm for numbers
    ... strings might be difficult though. ... Then the array to be ... You've invented the bucket sort. ...
    (comp.programming)
  • Re: Problem with this text-sorting algorithm
    ... I need to write an algorithm which will sort an array of text strings ... I'd done the sort. ... ByVal NumOfElements As Short) ...
    (microsoft.public.vb.general.discussion)
  • Re: Problem with this text-sorting algorithm
    ... I need to write an algorithm which will sort an array of text strings ... I'd done the sort. ... Dim temp As String ...
    (microsoft.public.vb.general.discussion)