[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Sorting objects...

Jason Burgett

3/8/2007 5:18:00 AM

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

--
Posted via http://www.ruby-....

6 Answers

Farrel Lifson

3/8/2007 5:20:00 AM

0

On 08/03/07, Jason Burgett <jasbur@gmail.com> wrote:
> 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)?
>

applications.sort_by{|object| object.ranking}

Justin Collins

3/8/2007 6:41:00 AM

0

Farrel Lifson wrote:
> On 08/03/07, Jason Burgett <jasbur@gmail.com> wrote:
>> 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)?
>>
>
> 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

Farrel Lifson

3/8/2007 7:10:00 AM

0

On 08/03/07, Justin Collins <collinsj@seattleu.edu> wrote:
> Farrel Lifson wrote:
> > On 08/03/07, Jason Burgett <jasbur@gmail.com> wrote:
> >> 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)?
> >>
> >
> > 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
>
>

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

Robert Retzbach

3/8/2007 8:19:00 AM

0

On 8 Mrz., 08:09, "Farrel Lifson" <farrel.lif...@gmail.com> wrote:
> On 08/03/07, Justin Collins <colli...@seattleu.edu> wrote:
>
>
>
>
>
> > Farrel Lifson wrote:
> > > On 08/03/07, Jason Burgett <jas...@gmail.com> wrote:
> > >> 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)?
>
> > > 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
>
> 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- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

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

Justin Collins

3/8/2007 9:12:00 AM

0

rretzbach wrote:
> On 8 Mrz., 08:09, "Farrel Lifson" <farrel.lif...@gmail.com> wrote:
>
>> On 08/03/07, Justin Collins <colli...@seattleu.edu> wrote:
>>
>>
>>
>>
>>
>>
>>> Farrel Lifson wrote:
>>>
>>>> On 08/03/07, Jason Burgett <jas...@gmail.com> wrote:
>>>>
>>>>> 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)?
>>>>>
>>>> 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
>>>
>> 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- Zitierten Text ausblenden -
>>
>> - 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


Timothy Goddard

3/8/2007 11:14:00 AM

0

On Mar 8, 9:19 pm, "rretzbach" <rretzb...@googlemail.com> wrote:
> On 8 Mrz., 08:09, "Farrel Lifson" <farrel.lif...@gmail.com> wrote:
>
>
>
> > On 08/03/07, Justin Collins <colli...@seattleu.edu> wrote:
>
> > > Farrel Lifson wrote:
> > > > On 08/03/07, Jason Burgett <jas...@gmail.com> wrote:
> > > >> 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)?
>
> > > > 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
>
> > 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- 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.