Click here to get back home

sorting a list of objects using one of their methods?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
sorting a list of objects using one of their methods? Grady Weyenberg 01-23-2008
Get Chitika Premium
Posted by Grady Weyenberg on January 23, 2008, 1:32 pm
Please log in for more thread options
Hi,

I have @list=($obj_1,$obj_2,...) where each $obj, although of different
classes, have a common inherited method, $obj->name.

I am trying to sort @list alphabetically using the value of $obj->name.
I have tried

@list = sort {$a->name cmp $b->name} @list;

but this fails with:
'Can't call method "name" without a package or object reference.'
and I'm not sure how to pass a reference in this context.

Will this be possible using Perl's sort directly on the object list, or
will I need to write my own sorting function?

Thanks,
Grady


Posted by John Bokma on January 23, 2008, 2:09 pm
Please log in for more thread options

> Hi,
>
> I have @list=($obj_1,$obj_2,...) where each $obj, although of different
> classes, have a common inherited method, $obj->name.
>
> I am trying to sort @list alphabetically using the value of $obj->name.
> I have tried
>
> @list = sort {$a->name cmp $b->name} @list;
>
> but this fails with:
> 'Can't call method "name" without a package or object reference.'
> and I'm not sure how to pass a reference in this context.

Sounds like you don't have only objects refs in your list, try to debug
with:

for my $item ( @list ) {

        print ref $item, "\n";
}

to see what you have in your list.

> Will this be possible using Perl's sort directly on the object list, or
> will I need to write my own sorting function?

AFAIK what you want should work, but it sounds like not all objects in
your list are actually object refs.

(Note: if your list is very long, and your name function is slow, you
might want to use a "Schwartzian Transform".)

--
John

Arachnids near Coyolillo - part 1
http://johnbokma.com/mexit/2006/05/04/arachnids-coyolillo-1.html

Posted by Florian Kaufmann on January 23, 2008, 4:23 pm
Please log in for more thread options
I can't help you with the error message. I only have an optimization
hint once it is running: If the method call is not cheap (in terms of
runtime) and if its result (per object) is const during the sort,
consider the Schwartzian Transform:
http://en.wikipedia.org/wiki/Schwartzian_transform#History.
It only calls the name subroutine once per object, whereas the naive
approach calls it many many times per object. To be more precise,
assuming that the complexity of perl's sort is n*log(n), name would be
called log(n) per object.

# untested example:

@list =
map { $_->[0] }
sort { $a->[1] cmp $b->[1]}
map { [ $_, $_->name ] }
@list;

Flo

Posted by Uri Guttman on January 23, 2008, 7:39 pm
Please log in for more thread options

FK> I can't help you with the error message. I only have an optimization
FK> hint once it is running: If the method call is not cheap (in terms of
FK> runtime) and if its result (per object) is const during the sort,
FK> consider the Schwartzian Transform:
http://en.wikipedia.org/wiki/Schwartzian_transform#History.

FK> It only calls the name subroutine once per object, whereas the
FK> naive approach calls it many many times per object. To be more
FK> precise, assuming that the complexity of perl's sort is n*log(n),
FK> name would be called log(n) per object.

nope. name is called once per object and cached which makes it O(N). it
doesn't change the growth curve of sort but factors out the method call
so it is O(N) and not O(N * log N).

and if you want the ST or the faster GRT and don't want to deal with
coding it, use Sort::Maker which does all the work for you.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Posted by John Bokma on January 23, 2008, 8:51 pm
Please log in for more thread options

>
> FK> I can't help you with the error message. I only have an
> optimization FK> hint once it is running: If the method call is not
> cheap (in terms of FK> runtime) and if its result (per object) is
> const during the sort, FK> consider the Schwartzian Transform:
> http://en.wikipedia.org/wiki/Schwartzian_transform#History.
>
> FK> It only calls the name subroutine once per object, whereas the
> FK> naive approach calls it many many times per object. To be more
> FK> precise, assuming that the complexity of perl's sort is
> n*log(n), FK> name would be called log(n) per object.
>
> nope. name is called once per object and cached which makes it O(N).
> it doesn't change the growth curve of sort but factors out the method
> call so it is O(N) and not O(N * log N).

I read FK as: naive requires O( log N) get_name calls per object. Each
comparison does 2 calls, so in total O( N * 2 * log N ) = O( N log N).

A simple test I wrote shows for:

10240 objects, shuffled, naive ~ 248, 086 calls, which is close to 10240
x 2 x log 10240.

Of course ST results in 10240 calls exactly.

--
John

Arachnids near Coyolillo - part 1
http://johnbokma.com/mexit/

Similar ThreadsPosted
Both Methods and Indexing for Objects? July 31, 2007, 9:18 am
sorting objects with "sort" and subroutine November 27, 2004, 11:55 am
sorting data - hash vs. list September 11, 2005, 4:41 pm
Accessing a container objects state from aggregated objects August 14, 2006, 8:50 pm
Overriding *all* methods August 25, 2004, 8:36 pm
listing all available methods May 22, 2007, 2:27 am
2 methods to get the domains's IP,but neither of them is good. August 1, 2004, 1:56 pm
croak conventions in methods? November 18, 2004, 12:55 am
Simple methods when using fields May 21, 2006, 6:03 am
How to find methods of Classes ? September 18, 2006, 2:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap