On Sat, Jan 17, 2009 at 12:28 AM, Daniel Firu <dannyfiru / gmail.com> wrote:
> On Jan 16, 3:03 pm, Daniel Firu <dannyf... / gmail.com> wrote:

First of all I would like to ask you some questions about the design
of classes and objects you have.
It seems that this is an instance method of a class, cause you are
using a couple of instance variables
(@data and @header). In this method, though, you are modifying @data,
cause you are doing a sort! on it.
Don't know if this side effect is on purpose or not, and if it has
some benefits or problems. It's also not clear to me why you sort the
data in a loop, each time with a different key. Maybe what you want is
to sort based on a set of keys. Your algorithm (sorting by the last
key, then sorting the result by the second to last, etc) might not
yield the same results as sorting based on all keys at the same time.
Maybe you want this (untested):

def sort(*keys, options = {})
# BTW, what is options? You never use it in your code, and the fact
that is outside of the parenthesis, but in the same line makes me
# wonder if you wanted that or what I just wrote
  indexes = keys.map {|k| @head.index(k)}
  sorted = @data.sort do |entry1, entry2|
    first, second = process_values(entry1.values_at(indexes),
entry2.values_at(indexes))
    first <=> second
  end
  Table.new({:header => @header, :data => sorted})
end

# Convert matching pairs of each array with to_f if both contain only digits
def process_values(first, second)
  regex = /\A(\d*)\z/
  first.zip(second).inject([[],[]]) do |(f,s), (v1,v2)|
    if (v1 =~ regex && v2 =~ regex)
      puts "Only digits found"
      v1 = v1.to_f
      v2 = v2.to_f
    end
    [f << v1, s << v2]
  end
end

This version is sorting based on an array that contains the value for each key.
Hope this helps,

Jesus.