Perl Advent Calendar 2008-12-24

Xmas sort of title

by Jerrad Pierce

The somewhat awkwardly named Sort::Half::Maker is a pretty interesting little chunk of code. A one-off developed by the author to dump data with important keys first, it allows you to "sort by example" as in our code sample which yields:


Other uses for it might be to ensure that in a sorted list of contestants, those who placed sort accordingly, while everyone else sorts alphabetically; perhaps with no-shows or disqualified individuals bringing up the rear. This would require a multi-step sort where placed contestants are sorted by rank into a start, whose results are then referenced by the ½ maker.

   1 use Sort::Half::Maker 'make_halfsort';
   3 #Hooray for tuples
   4 my @order = (Dasher=>Dancer,
   5 	     Prancer=>Vixen,
   6 	     Comet=>Cupid,
   7 	     Dunder=>Blixem);
   9 #…and a Rudy to lead!
  10 unshift @order, 'Rudolph';
  13 #Pseudo-random order
  14 my @data = qw/Dunder Blixem Dasher Dancer Prancer Vixen
  15               Rudolph Comet Cupid Olive Chet/;
  17 $sub = make_halfsort(start => \@order);
  18 print join(',', sort $sub @data), "\n";

See Also

Sort::ByExample - without end, but with fallback, transmogrification and dependencyitis

View Source (POD)