in reply to MsExcel like Multi Column Sorting

From perlfaq4 - How do I sort an array by (anything)?:

If you need to sort on several fields, the following paradigm is useful.

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

(Also, it's "Perl", not "PERL")

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: MsExcel like Multi Column Sorting
by bart (Canon) on Dec 01, 2006 at 12:23 UTC
    To apply this code to the data structure as given by the op, this gives:
    @sorted = sort { $a->{key2} cmp $b->{key2} || $a->{num1} <=> $b->{num1} || $a->{key3} cmp $b->{key3} || } @data;
    Note that to sort descending for one column, just swap $a and $b in the comparison expression, for that column.
A reply falls below the community's threshold of quality. You may see it by logging in.