SPUG: Another stupid question, hash sorting

Jeremy Kahn kahn at cpan.org
Tue Jul 2 19:19:05 CDT 2002


Don't forget that you can use the || and <=> and cmp operators to
shorthand a lot of this:

  # untested [snipped from Jonathan's code]

  my @a = ({Score => 5, Name => 'ann'},
  	   {Score => 5, Name => 'the other ann'},
 	   {Score => 2, Name => 'lars'});

  # custom sort provided in block
  my @sorted =
    sort { $a->{Score} <=> $b->{Score}
                 ||
              $a->{Name} cmp $b->{Name} }  @a;

This makes the most sense usually when you're doing a simple bi-level
sort, but it can be chained if you need to.

--jeremy


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list