# Sorting objects

**URL:** <https://www.ruby-forum.com/t/sorting-objects/88100>\
**Category:** Ruby\
**Created:** [March 8, 2007, 6:17am UTC](https://www.ruby-forum.com/t/sorting-objects/88100 "2007-03-08T06:17:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [March 8, 2007, 6:17am UTC](https://www.ruby-forum.com/t/sorting-objects/88100/1 "2007-03-08T06:17:44Z")

</div>

I have a series of objects in an array. Each object corresponds to an  
application and has several attributes. For instance I can call:

@application.name  
@application.icon  
@application.ranking

“ranking” is always an integer. So if I have an array of these objects,  
say, @allApplications how do I sort using each application’s ranking  
(@application.ranking)?

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [March 8, 2007, 6:21am UTC](https://www.ruby-forum.com/t/sorting-objects/88100/2 "2007-03-08T06:21:00Z")

</div>

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

> 

applications.sort\_by{|object| object.ranking}

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [March 8, 2007, 7:42am UTC](https://www.ruby-forum.com/t/sorting-objects/88100/3 "2007-03-08T07:42:06Z")

</div>

Farrel L. wrote:

> > (@application.ranking)?
> 
> applications.sort\_by{|object| object.ranking}

Another way would be to add a \<=\> method

class Application  
def \<=\> other  
self.ranking \<=\> other.ranking  
end  
end

-Justin

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [March 8, 2007, 9:21am UTC](https://www.ruby-forum.com/t/sorting-objects/88100/4 "2007-03-08T09:21:14Z")

</div>

On 8 Mrz., 08:09, “Farrel L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Another way would be to add a \<=\> method
> 
> ```
> end    
> user system total real
> 
> ```
> 
> Sort 20.782000 0.031000 20.813000 ( 20.844000)  
> Sort By 6.687000 0.000000 6.687000 ( 6.687000)
> 
> Farrel- Zitierten Text ausblenden -
> 
> - Zitierten Text anzeigen -

I don’t understand, can someone please explain this loss of speed?

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [March 8, 2007, 8:10am UTC](https://www.ruby-forum.com/t/sorting-objects/88100/5 "2007-03-08T08:10:08Z")

</div>

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

> > > say, @allApplications how do I sort using each application’s ranking  
> > > self.ranking \<=\> other.ranking  
> > > end  
> > > end
> 
> -Justin

Using only \<=\> does have some speed consequences:

require ‘benchmark’  
srand

class Application  
attr\_reader :ranking  
def initialize  
@ranking = rand  
end  
def \<=\>(other)  
@ranking \<=\> other.ranking  
end  
end

applications = Array.new(1000000){Application.new}

Benchmark.bm do |bmark|  
bmark.report(“Sort”) { applications.sort }  
bmark.report(“Sort By”) {applications.sort\_by {|o| o.ranking}}  
end

C:\Documents and Settings\flifson\Desktop\>ruby sort\_test.rb  
user system total real  
Sort 20.782000 0.031000 20.813000 ( 20.844000)  
Sort By 6.687000 0.000000 6.687000 ( 6.687000)

Farrel

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [March 8, 2007, 12:15pm UTC](https://www.ruby-forum.com/t/sorting-objects/88100/6 "2007-03-08T12:15:33Z")

</div>

On Mar 8, 9:19 pm, “rretzbach” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Another way would be to add a \<=\> method
> 
> > ```
> > end
> > user system total real
> > 
> > ```
> > 
> > Sort 20.782000 0.031000 20.813000 ( 20.844000)  
> > Sort By 6.687000 0.000000 6.687000 ( 6.687000)
> 
> > Farrel- Zitierten Text ausblenden -
> 
> > - Zitierten Text anzeigen -
> 
> I don’t understand, can someone please explain this loss of speed?

Sorting compares pairs of objects many more times than the number of  
objects actually being sorted. The \<=\> method extracts the ranking  
from the objects each time a pair is compared. The sort\_by method only  
extracts the ranking from each object once then compares these. The  
time difference represents the overhead of the \<=\> method call and the  
time required to retrieve instance variables for each of the pair.

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [March 8, 2007, 10:13am UTC](https://www.ruby-forum.com/t/sorting-objects/88100/7 "2007-03-08T10:13:22Z")

</div>

rretzbach wrote:

> > > > > say, @allApplications how do I sort using each application’s ranking  
> > > > > end  
> > > > > def initialize  
> > > > > bmark.report(“Sort”) { applications.sort }
> > 
> > - Zitierten Text anzeigen -
> 
> I don’t understand, can someone please explain this loss of speed?

I’m guessing it has to do with Enumerable#sort\_by doing the key caching  
thing?

-Justin
