"Wilka" <jvyxn / evghnyvfgvp.pbz> wrote:
>
>Does Ruby already have an compare method that takes into account numbers
>inside a string? i.e.
>
>strings = Array.new
>strings.push "Name 300"
>strings.push "Name 1"
>strings.push "Name 10"
>strings.push "Name 2"
>
>strings.sort do |a, b|
>      # something to put here to sort like a human would
>end

Not as such, but it can be done fairly easily:

    def sort_split (s)
      pieces = s.scan(/(\D*)(\d*)/)
      pieces.each {|elem| elem[1] = elem[1].sub(/^0*/, '').to_i}
      pieces.push s
    end

    things = (1..20).to_a.map {|i| "thing #{i}"}
    things.push "thing ", "hello", "world"
    puts things.map {|e| sort_split(e)} .sort.map {|e| e[-1]}

I think someone is trying to put together a cookbook
somewhere.  If so, then this may be a good example.
(Trust me, this exercise is much, much harder to do
in Perl!)

Cheers,
Ben
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com