On Jan 16, 3:03=A0pm, Daniel Firu <dannyf... / gmail.com> wrote:
> Hi All,
>
> Long time reader -- first time poster. =A0Basically, I have a bunch of
> data that is sucked in from a file. =A0From reading the pick axe book,
> it is my understanding that this data by default will be a string even
> if the contents of that string are a bunch of digits. =A0Well, in the
> case that a string is solely composed of digits, I want to convert
> them to a float for the purpose of using the <=3D> operator. =A0I'll just
> go ahead and throw in my method and let you guys tear it a part. =A0The
> goal of the function is to soft my array of arrays based on a
> particular element defined by a 'key'. =A0Maybe there is an easier way
> to do this all together.
>
> =A0 def sort(*keys) options =3D {}
> =A0 =A0 regex =3D "^(\d)*$"
> =A0 =A0 newData =3D Array.new
> =A0 =A0 keys.reverse!
> =A0 =A0 keys.each do |key|
> =A0 =A0 =A0 newData =3D @data
> =A0 =A0 =A0 keyIndex =3D @header.index(key)
> =A0 =A0 =A0 newData.sort! do |entry1, entry2|
> =A0 =A0 =A0 =A0 if (!(entry1[keyIndex].match(regex).nil? && entry2
> [keyIndex].match(regex).nil?))
> =A0 =A0 =A0 =A0 =A0 print "I found only digits\n"
> =A0 =A0 =A0 =A0 =A0 entry1[keyIndex].to_f <=3D> entry2[keyIndex].to_f
> =A0 =A0 =A0 =A0 else
> =A0 =A0 =A0 =A0 =A0 #print "I found something other than digits\n"
> =A0 =A0 =A0 =A0 =A0 entry1[keyIndex] <=3D> entry2[keyIndex]
> =A0 =A0 =A0 =A0 end
> =A0 =A0 =A0 end
> =A0 =A0 end
> =A0 =A0 newTable =3D Table.new({
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0:header =3D> @head=
er,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0:data =3D> newData
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0})
> =A0 =A0 newTable
> =A0 end
>
> Thanks in advance for you feedback. =A0Feel free to be brutal -- I have
> thick skin and am still learning.

Hi All,

I went ahead and changed my method to:

  def sort(*keys) options =3D {}
    regex =3D Regexp.new('^\d+$')
    newData =3D Array.new
    keys.reverse!
    keys.each do |key|
      newData =3D @data
      keyIndex =3D @header.index(key)
      newData.sort! do |entry1, entry2|
        if (entry1[keyIndex] =3D~ regex && entry2[keyIndex] =3D~ regex)
          #print "I found only digits\n"
          entry1[keyIndex].to_f <=3D> entry2[keyIndex].to_f
        else
          #print "I found something other than digits\n"
          entry1[keyIndex] <=3D> entry2[keyIndex]
        end
      end
    end
    newTable =3D Table.new({
                           :header =3D> @header,
                           :data =3D> newData
                         })
    newTable
  end

Now the question still remains if there is a better way to do this
sorting.