If we knew in advance how many elements each of your sub-arrays were going to have, and if it were a fairly small number, we could do this with an inlined sort routine, perhaps something along these lines (assuming three elements in each sub-array):

use Data::Dumper; print Dumper(+{ unsorted => \@array }); @array = sort { $$a[0] <=> $$b[0] or $$a[1] <=> $$b[1] or $$a[2] <=> $$b[2] } @array; use Data::Dumper; print Dumper(+{ sorted_on_first_three_elements => \@array });

Note: This code is untested.

This is pretty basic, and works when we don't mind listing each of the sort criteria.

However, if we don't know how many elements there are, or if there are a whole bunch, it may be more practical to have the sort routine loop over the subarray indices from 0 to whatever, returning as soon as it has an answer. (The return values should match what <=> would return; see perlop for further information about that.)

use Data::Dumper; print Dumper(+{ unsorted => \@array }); @array = sort { for my $i (0 .. $#a) { if (not ($$a[$i] == $$b[$i])) { return $$a[$i] <=> $$b[$i]; } } return 0; } @array; use Data::Dumper; print Dumper(+{ sorted => \@array });

Note: This code is untested.

Note too that the way you define the AoA feels kind of awkward, like maybe you aren't really comfortable with Perl data structures yet. That's fine, at first, but you will want to work toward being more comfortable with Perl data structures until you are able to do things like this:

my @user = ( # [ username, fullname, authlevel, { otherinfo } ], [ 'george', 'George Jetson', $AUTH_EMPLOYEE, { supervisor => 'spaceley', pet => 'astro', } ], [ 'astro', 'Astro', $AUTH_BASIC, { supervisor => 'george', } ], [ 'hhoyt', 'Herman Hoyt', $AUTH_PROFESSOR, { supervisor => 'amcclain', department => 'theology', } + ], # and so on and so forth );

In reply to Re: sorting array of array references with multiple dereferenced array elements by jonadab
in thread sorting array of array references with multiple dereferenced array elements by onlyIDleft

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.