# Array#sort -block with conditions for \<=\>'ing

**URL:** <https://www.ruby-forum.com/t/array-sort-block-with-conditions-for-ing/88145>\
**Category:** Ruby\
**Created:** [March 8, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/array-sort-block-with-conditions-for-ing/88145 "2007-03-08T14:42:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![carp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@carp](https://www.ruby-forum.com/u/carp)\
**Post date:** [March 8, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/array-sort-block-with-conditions-for-ing/88145/1 "2007-03-08T14:42:41Z")

</div>

Hello Rubyists,

I want to sort an array of objects of the same class (MyClass). The  
array should be sorted by arbitrary attributes of that class, but the  
problem is that one attribute can be nil.

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

Does anyone know how to do it? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![carp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@carp](https://www.ruby-forum.com/u/carp)\
**Post date:** [March 8, 2007, 2:53pm UTC](https://www.ruby-forum.com/t/array-sort-block-with-conditions-for-ing/88145/2 "2007-03-08T14:53:03Z")

</div>

On 08/03/07, carp \_\_ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> }  
> MyClass with MyClass failed).
> 
> Does anyone know how to do it? Thanks in advance!

Define \<=\> on your class:

class MyClass  
def \<=\>(other)  
if @some\_attribute.nil?  
return -1  
elsif @other.some\_attribute.nil?  
return 1  
else  
@some\_attribute \<=\> other.some\_attribute  
end  
end  
end

Farrel

---

<div class="post-metadata">

**Author:** ![carp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@carp](https://www.ruby-forum.com/u/carp)\
**Post date:** [March 8, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/array-sort-block-with-conditions-for-ing/88145/3 "2007-03-08T15:05:05Z")

</div>

On Thu, Mar 08, 2007 at 10:42:44PM +0900, carp \_\_ wrote:

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

---

<div class="post-metadata">

**Author:** ![carp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@carp](https://www.ruby-forum.com/u/carp)\
**Post date:** [March 9, 2007, 1:45am UTC](https://www.ruby-forum.com/t/array-sort-block-with-conditions-for-ing/88145/4 "2007-03-09T01:45:40Z")

</div>

On Mar 8, 5:52 am, “Farrel L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> end  
> end

I find your style of mixing imperative and functional return  
parameters odd. I would have expected either:

if foo  
-1  
elsif bar  
1  
else  
x \<=\> y  
end

OR

if foo  
return -1  
elsif bar  
return 1  
else  
return x \<=\> y  
end

but not the combination you have above. FWIW, I favor the former  
style, as it’s just a hair faster.

---

<div class="post-metadata">

**Author:** ![carp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@carp](https://www.ruby-forum.com/u/carp)\
**Post date:** [March 8, 2007, 5:09pm UTC](https://www.ruby-forum.com/t/array-sort-block-with-conditions-for-ing/88145/5 "2007-03-08T17:09:07Z")

</div>

Thank you Brian, the solution you offered solved my problems!
