John Woods
10/12/2007 2:34:00 AM
If I understand what you're after, I'd say in the 2nd & 3rd conditions,
you'd want to always treat nil as being less than a non-nil value. So
always return -1 and 1 respectively. As an aside, you can get rid of the
!x.nil? checks because the previous conditions assure that state...
cset_arr = cset_arr.sort do |x, y|
if x[5].nil? && y[5].nil?
x[6] <=> y[6]
elsif x[5].nil?
-1
elsif y[5].nil?
1
else
x[5] <=> y[5]
end
end
-----Original Message-----
From: Mr_Tibs
Sent: 10/11/2007 07:15 PM
> Hi,
>
> I have a [m X n] double-dimensional array in Ruby and I'm trying to
> sort the entries (rows) by the 6-th column, which is a DateTime
> object. Things get a little bit complicated, since I might have a nil
> value in that column. If that happens, then I want to sort by the 7-th
> column.
> This is what I have so far:
>
> cset_arr = cset_arr.sort do |x, y|
> if x[5].nil? && y[5].nil?
> x[6] <=> y[6]
> elsif x[5].nil? && !y[5].nil?
> y[5] # ???
> elsif !x[5].nil? && y[5].nil?
> x[5] # ???
> elsif !x[5].nil? && !y[5].nil?
> x[5] <=> y[5]
> end
> end
>
> For the second and third if statements, I don't know how to "select"
> that value. So, for example, if both 6-th columns are nil, then
> compare by the 7-th column (first if). But, if the first record has a
> nil 6-th column and the second record has a non-nil 6-th column, then
> how do I set the second record as being the "selected" one of the two?
> The current form (e.g. y[5]) does not work.
>
> Thanks,
> Tiberiu
>
>
>
>