[Omaha.pm] sort hackery

Miller, Scott L (Omaha Networks) Scott.L.Miller at hp.com
Thu Oct 27 08:21:20 PDT 2005


Two questions for you: 
    How large is the array you are sorting?
    Is response time important?

If the answers are "quite large" and "yes", then there are several
things you can do to speed up/optimize your sorting routine.

If the answers don't match, then it's likely not worth the maintenance
problems that come with the optimizations.

For those that might be interested in reading more about sorting
optimizations in Perl here's a short list of good material:
  A good introductory Perl sorting tutorial
   http://www.perlmonks.com/?node_id=128722 Resorting to sorting

  A discussion about when it does and doesn't make sense to use
"optimized"
    sort routines
   http://www.perlmonks.com/?node_id=447633 Choosing the right sort
[method]

  A tutorial that attempts to make transformation sorts more accessible
to
    those new to the concept.
   http://www.perlmonks.com/?node_id=457993 Understanding xformation
sorts

Full disclosure: the last link was written by me.

-Scott

-----Original Message-----
From: omaha-pm-bounces at pm.org [mailto:omaha-pm-bounces at pm.org] On Behalf
Of Jay Hannah
Sent: Tuesday, October 25, 2005 10:43 AM
To: omaha-pm at pm.org
Subject: [Omaha.pm] sort hackery


So I've got this array of hashrefs that was built like this:

   push @arr, { code=>$prop, desc=>$desc, };

And I want to sort them alphabetically based of desc.

So I do this:

  # Sort the list by descriptions
  @arr = sort sort_by_desc @arr;

  sub sort_by_desc {
     $a->{desc} cmp $b->{desc}; 
  }

And it worked, 2nd try. 

Yay! It appears I've learned a little Perl over the years. -grin-

Custom sorting methods in Perl rule. Do any other languages allow you to
define custom arbitrary sorting rulesets so easily?

j

_______________________________________________
Omaha-pm mailing list
Omaha-pm at pm.org
http://mail.pm.org/mailman/listinfo/omaha-pm


More information about the Omaha-pm mailing list