Sorting array with nils

R

RK Sentinel

I have been browsing but given answers are not suiting me.

I hava a double dim array like a dataset.
I thus *cannot* use compact since that would knock off rows. Some
answers say a nil cannot be compared to another datatype so the question
of putting such rows at the beginning or end does not arise.

I am trying to check for nils and using a "false/true" or -1/1 etc but
all are giving errors. Could someone help me out with this.

def sort categ, column, descending=false
d = get_records_for_category categ
d = d.sort { |y,x|
if descending
if x[column].nil?
true # <<<<------------- errors
elsif y[column].nil?
false
else
x[column] <=> y[column]
end
else
if x[column].nil?
1 # <<<-------------- gives errors
elsif y[column].nil?
-1
else
y[column] <=> x[column]
end
end
}
return d
end

Since its a double dim array, i cannot say what the datatype will be:
can be string, fixnum, date or true/false.

I figured since "A" <=> "B" reutrns -1,0 or 1, i should also put one of
these in the loop but it throws an error.

thanks.
 
S

Stefan Rusterholz

(RK) Sentinel said:
def sort categ, column, descending=false
d = get_records_for_category categ
d = d.sort { |y,x|
if descending
if x[column].nil?
true # <<<<------------- errors --- yes of course, <=> expectes -1,0,1, not true/false
elsif y[column].nil?
false # <<<<------------- errors too, for the same reason
else
x[column] <=> y[column]
end
else
if x[column].nil?
1 # <<<-------------- gives errors --- no, should not
elsif y[column].nil?
-1
else
y[column] <=> x[column]
end
end
}
return d
end

A solution (untested):
class Uncomparable
def initialize(always_less=true)
@compare = always_less ? -1 : 1
end

def <=>(other)
Uncomparable === other ? 0 : @compare
end
end
...
d.sort_by { |row| row[column] || Uncomparable(!descending) }
...

Depending on what end you want the nils, you have to change !descending
to just descending.

Regards
Stefan
 
R

(RK) Sentinel

(RK) Sentinel said:
I have been browsing but given answers are not suiting me.
I figured since "A" <=> "B" reutrns -1,0 or 1, i should also put one of
these in the loop but it throws an error.

thanks.

My apologies:

did some further debugging and one Time value format was different from
the others.
It is okay to use -1 and 1 after checking for nil?.

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top