[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

sort double-dimensional array by column

Mr_Tibs

10/12/2007 2:12:00 AM

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

5 Answers

John Woods

10/12/2007 2:34:00 AM

0


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
>
>
>
>

mortee

10/12/2007 3:49:00 AM

0

Mr_Tibs

10/12/2007 8:52:00 PM

0

Hi James,

Thanks for the reply. It was what I needed (with a small correction:
inverse -1 and 1). I didn't read the Array.sort doc properly and
didn't know what I supposed to return.

Thanks,
Tiberiu

Mr_Tibs

10/12/2007 9:00:00 PM

0

Thanks mortee. That sort of worked. Unfortunately, it puts those
entries that have the 6-th column = nil, at the start of the sorted
array (instead of at the end). I don't really understand the technique
used. You are sorting by row, but you are somehow defining and using
an array with 2 elements (that's what the square brackets do, right?).

Tiberiu

mortee

10/12/2007 10:28:00 PM

0