in reply to Classic Sort Algorithm

This is a FAQ: How do I sort an array by (anything)?. (See also  perldoc perlfaq4) You want the

@sorted = sort { field1($a) <=> field1($b) || field2($a) cmp field2($b) || field3($a) cmp field3($b) } @data;

'paradigm' for multi-key sorting.

Replies are listed 'Best First'.
Re^2: Classic Sort Algorithm
by BoulderBuff64 (Novice) on Jun 20, 2011 at 23:05 UTC
    I'm not sure what it means when it refers to field1($a). When I insert this into my code I get a Undefined subroutine &main::field1 called at error.
      I'm not sure what it means when it refers to field1($a). When I insert this into my code I get a Undefined subroutine &main::field1 called at error.
      Yes. It was left to the reader, you, to define a method that extracts field1 from a line of data. Maybe calling the function get_field1() would have been clearer.

      Note that when perl sorts a group of things it has to compare each thing to the other things in the group many times. When comparing things perl assigns two of the things to the global variables $a and $b.