On Thu, Mar 08, 2007 at 10:42:44PM +0900, carp __ wrote:
> The code looks like this:
> 
> my_array.sort {
>   |x,y| x.some_attribute <=> y.some_attribute
> }
> 
> Naturally, if some_attribute is nil, the comparison failed (since nil is
> not comparable). I am  looking for something like this:
> 
> my_array.sort {
>   |x,y| x.some_attribute <=> y.some_attribute unless
> (x.some_attribute.nil? or y.some_attribute.nil?)
> }
> 
> which I have tried without having success (Error was: comparison of
> MyClass with MyClass failed).

You have to return something from the comparison. Try something like:

my_array.sort { |x,y|
  x1 = x.some_attribute
  y1 = y.some_attribute
  if (not x1.nil?) and (not y1.nil?)
    x1 <=> y1
  elsif x1.nil? and y1.nil?
    0
  elsif x1.nil?
    -1
  else
    1
  end
}