# Defining sort \<=\> for a class

**URL:** <https://www.ruby-forum.com/t/defining-sort-for-a-class/146709>\
**Category:** Ruby\
**Created:** [August 24, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/defining-sort-for-a-class/146709 "2008-08-24T14:06:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 24, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/defining-sort-for-a-class/146709/1 "2008-08-24T14:06:57Z")

</div>

If i have a class

class Example

attr\_accessor :value

def initialize  
@value = 0  
end

end

and I have an array of examples and want to sort them based on value how  
do i define a \<=\> for my class. I know its possible as it was a side  
comment on an earlier post. Seeing as this is a totally different  
subject to that post i decided to ask in a new post - it will be easier  
for others who may have the same questoin to search for.

i tried this

class Example

attr\_accessor :value

def initialize  
@value = 0  
end

def \<=\>(other\_example)  
@value \<=\> other\_example  
end  
end

but i keep getting an error of undefined method for nill class when i  
try to call it like this with an array of example objects

ojects.sort do |one, another|  
one.\<=\>(another)  
end

i also tried  
ojects.sort do |one, another|  
one \<=\>(another)  
end

(i.e no period between one and \<=\>)

where am i going wrong???

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 24, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/defining-sort-for-a-class/146709/2 "2008-08-24T14:32:34Z")

</div>

Heya!

On Sun, Aug 24, 2008 at 2:03 PM, Adam A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> i tried this
> 
> class Example
> 
> attr\_accessor :value
> 
> def initialize  
> @value = 0  
> end

Here’s the error:

> def \<=\>(other\_example)  
> @value \<=\> other\_example  
> end

You want to sort by the value, so you have to compare the values:

def \<=\>(other\_example)  
@value \<=\> other\_example.value  
end

A simple example:  
a = Example.new  
b = Example.new  
a.value = 10  
arr = [a,b]

# =\> [#\<Example:0x2b5d6a8 @value=10\>, #\<Example:0x2b5b7cc @value=0\>]

arr.sort!

# =\> [#\<Example:0x2b5b7cc @value=0\>, #\<Example:0x2b5d6a8 @value=10\>],

means arr = [b, a]

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 24, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/defining-sort-for-a-class/146709/3 "2008-08-24T15:58:06Z")

</div>

ahh so the \<=\> thing is called implicitly, i dont have to call it  
automatically in my code. I just do sort!.

thanks very much, ill go away and fix it.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 24, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/defining-sort-for-a-class/146709/4 "2008-08-24T16:42:31Z")

</div>

On Sun, Aug 24, 2008 at 3:53 PM, Adam A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ahh so the \<=\> thing is called implicitly, i dont have to call it  
> automatically in my code. I just do sort!.

Yeah, it is. Enumerable#sort calls implicitly the \<=\> method of the  
elements.

> thanks very much, ill go away and fix it.

You’re very welcome

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 24, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/defining-sort-for-a-class/146709/5 "2008-08-24T16:55:26Z")

</div>

Adam A. wrote:

> ahh so the \<=\> thing is called implicitly, i dont have to call it  
> automatically in my code. I just do sort!.

Alternately, use sort\_by to repurpose the implicit \<=\>. Examples:

.sort\_by{rand} # a shuffle  
.sort\_by{|x| -x } # reverse order  
.sort\_by{|r| r.name.downcase } # ascibetic order by name

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 24, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/defining-sort-for-a-class/146709/6 "2008-08-24T17:05:23Z")

</div>

> .sort\_by{|r| r.name.downcase } # ascibetic order by name

Actually, sorting by a downcase is not strictly an asciibetic sort. It’s  
a cheap  
form of “collation”, where an expensive collation uses your locale to  
decipher  
its dictionary order rules. Example: Packing McDonalds in with  
MacDonalds…

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 24, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/defining-sort-for-a-class/146709/7 "2008-08-24T17:43:15Z")

</div>

yes ive seen sort\_by a few times now. ill start using that in my code.  
thanks everyone again.
