Pathologically Eclectic Rubbish Lister PerlMonks

### Re: sorting array of array references with multiple dereferenced array elements

 on Sep 23, 2014 at 12:51 UTC ( #1101632=note: 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
);

Replies are listed 'Best First'.
Re^2: sorting array of array references with multiple dereferenced array elements
by LanX (Cardinal) on Sep 23, 2014 at 13:16 UTC
This

> for my \$i (0 .. \$#a) {

looks wrong: \$a is an array ref there is no @a involved.

IIRC \$#\$a should do.

Note This code is untested. ;)

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

##### update

now tested! :)

```  DB<109> \$a=[1..10]                            # arrref
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

DB<110> \$#\$a
=> 9

DB<111> \$a=[map [0..\$_],0..3 ]                # AoA
=> [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]

DB<112> \$#{\$a->[2]}
=> 2

DB<113> @b=(1..5)                             # @array
=> (1, 2, 3, 4, 5)

DB<114> \$#b
=> 4

though I couldn't find a good documentation yet! :(

Re^2: sorting array of array references with multiple dereferenced array elements
by onlyIDleft (Scribe) on Sep 23, 2014 at 15:04 UTC

Re^2: sorting array of array references with multiple dereferenced array elements
by onlyIDleft (Scribe) on Sep 23, 2014 at 13:48 UTC

Thank you for your replies, jonadab (and lanX). I have had a long 1 year break from coding, and even back then, never progressed until data structures, just until arrays and hashes. ButI will take you up on your suggestions, in the future :)

Mneawhile, I tried your syntax, jonadab, for comparing using just one or morecriterion, BUT get the error message: "Not a SCALAR reference at test.pl line 254". Any thoughts on why this may be happening / how to fix it? Thank yoU!

Create A New User
Node Status?
node history
Node Type: note [id://1101632]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2021-01-25 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The STEM quote I most wish I'd made is:

Results (270 votes). Check out past polls.

Notices?