# Re: Fixnum \<=\>

**URL:** <https://www.ruby-forum.com/t/re-fixnum/102073>\
**Category:** Ruby\
**Created:** [November 24, 2005, 1:24pm UTC](https://www.ruby-forum.com/t/re-fixnum/102073 "2005-11-24T13:24:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vnainar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/839c29/32.png) [@vnainar](https://www.ruby-forum.com/u/vnainar)\
**Post date:** [November 24, 2005, 1:24pm UTC](https://www.ruby-forum.com/t/re-fixnum/102073/1 "2005-11-24T13:24:16Z")

</div>

I may be missing something obvious . Why is the ‘\<=\>’ operator not  
called when there is no block supplied to sort ?.The Pickaxe book  
says ( apropos Array.sort ):  
" Comparisons for the sort will be done using the \<=\> operoator OR  
using an optional code block"  
How is the comparison done in the second case ?

class Fixnum  
alias oldcmp \<=\>  
def \<=\>(other)  
puts " \<=\> called with #{self} and #{other} "  
oldcmp(other)  
end  
end

p [9,1,7,3,5].sort { |a, b| a \<=\> b}  
p [99,11,77,33,55].sort

nainar

---

<div class="post-metadata">

**Author:** ![vnainar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/839c29/32.png) [@vnainar](https://www.ruby-forum.com/u/vnainar)\
**Post date:** [November 24, 2005, 1:40pm UTC](https://www.ruby-forum.com/t/re-fixnum/102073/2 "2005-11-24T13:40:23Z")

</div>

> > > > > “v” == v nainar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

v\> How is the comparison done in the second case ?

ruby has some optimizations : if the 2 elements that it need to compare  
are both Fixnum, or both Strings it make internally the comparison and  
don’t call \<=\>

Guy Decoux

---

<div class="post-metadata">

**Author:** ![vnainar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/839c29/32.png) [@vnainar](https://www.ruby-forum.com/u/vnainar)\
**Post date:** [November 24, 2005, 1:40pm UTC](https://www.ruby-forum.com/t/re-fixnum/102073/3 "2005-11-24T13:40:23Z")

</div>

v.nainar wrote:

> ```
> puts " <=> called with #{self} and #{other} "
> oldcmp(other)
> 
> ```
> 
> end  
> end
> 
> p [9,1,7,3,5].sort { |a, b| a \<=\> b}  
> p [99,11,77,33,55].sort

AFAIK it’s an internal optimization.

```
robert
```

---

<div class="post-metadata">

**Author:** ![vnainar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/839c29/32.png) [@vnainar](https://www.ruby-forum.com/u/vnainar)\
**Post date:** [November 24, 2005, 3:29pm UTC](https://www.ruby-forum.com/t/re-fixnum/102073/4 "2005-11-24T15:29:56Z")

</div>

On Thu, Nov 24, 2005 at 09:37:25PM +0900, Robert K. wrote:

> v.nainar wrote:
> 
> > I may be missing something obvious . Why is the ‘\<=\>’ operator not  
> > called when there is no block supplied to sort ?.The Pickaxe book  
> > says ( apropos Array.sort ):  
> > " Comparisons for the sort will be done using the \<=\> operoator OR  
> > using an optional code block"  
> > How is the comparison done in the second case ?  
> > …

> AFAIK it’s an internal optimization.
> 
> ```
> robert
> 
> ```

thanks !  
nainar

---

<div class="post-metadata">

**Author:** ![vnainar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/839c29/32.png) [@vnainar](https://www.ruby-forum.com/u/vnainar)\
**Post date:** [November 25, 2005, 12:58am UTC](https://www.ruby-forum.com/t/re-fixnum/102073/5 "2005-11-25T00:58:56Z")

</div>

v.nainar wrote:

> I may be missing something obvious . Why is the ‘\<=\>’ operator not  
> called when there is no block supplied to sort ?.The Pickaxe book

see:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/123637](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/123637)

there are optimisations in the core classes such as String which also  
currently affect user-defined subclasses. you can make sure that your  
own own \<=\> operator is called by using:

something.sort { | i, j | i \<=\> j }

instead of

something.sort

alex
