Perl Sort Array of Arrays


I have an Array of Arrays.

I need to sort the entire array on the 12 field in each array. The field is a datefield, so newer date at the top and older date at the bottom.

I tried the normal sort functions and I cannot figure it out. Any help or pointing to docs on the web is much appreciated.



Write your own sort function that uses the ‘12 field’ in the comparison

@AoA = sort { $a->[12] > $b->[12] } @AoA

An example sort function using the contents of the 12th (13th) item… but you didn’t say what your date format was. Use a CPAN Date module.

General docs are at

PS hubby wrote this, I’m not awesome enough to write my own sort functions in Perl : )


use strict;
use warnings;
use 5.010;

my @AoA = (
    ['a', '2003-11-09', 'b'],
    ['c', '2003-11-01', 'd'],
    ['e', '2002-11-01', 'f'],
    ['g', '2003-10-01', 'h'],
    ['g', '2002-10-01', 'h'],

my @new_arr = sort {$a->[1] cmp $b->[1]} @AoA;

for (@new_arr) {
    say "@$_";

g 2002-10-01 h
e 2002-11-01 f
g 2003-10-01 h
c 2003-11-01 d
a 2003-11-09 b