Section 9.5. Multi-Level Sort with the Schwartzian Transform


9.5. Multi-Level Sort with the Schwartzian Transform

If we need to sort on more than one criterion, the Schwartzian Transform is still up to the task.

 my @output_data =   map $_->[0],   sort { SORT COMPARISON USING $a->[1] AND $b->[1] or           ANOTHER USING $a->[2] AND $b->[2] or       YET ANOTHER USING $a->[3] AND $b->[3] }   map [ $_, SOME FUNCTION OF $_, ANOTHER, YET ANOTHER ],   @input_data; 

This code skeleton has a three-level sort comparison, using three computed values saved in the anonymous array (alongside the original data item to be sorted, which always comes first).




Intermediate Perl
Intermediate Perl
ISBN: 0596102062
EAN: 2147483647
Year: N/A
Pages: 238

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net