Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-18 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found