The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Sort::ArrayOfArrays - Perl extension for sorting an array of arrays


  use Sort::ArrayOfArrays;
  my $sort = Sort::ArrayOfArrays->new({
    results => [
      [1 .. 10],
      [10 .. 1],
    sort_column => -1,
  my $sorted = $sort->sort_it;

  # several examples are in the test scripts that came with the package in the t/ directory


Sort::ArrayOfArrays was written to sort an arbitrary array of arrays, in powerful, different ways.


Any of the properties below can be set in your object. This can easily be done by passing a hash ref to new. header_row => set to 1 if you have a header row in $self->{results}

sort_code => how to sort on each column, can be a code ref - a code ref that gets run through sort (sorry, currently no multi-column sort of code ref) a hash ref - the key is the column number the value is described below, like sort_code => { 0 => 'aa', 2 => 'rd', 4 => 'nd', } an array ref - a list of values as described below, where each position corresponds to the respective column sort_code => [ 'aa', 'la', 'da', ] the sort code values (when not a code ref) are two digits,

  the first digit possibilities are
    a - alphabetical sort
    n - numerical sort
    r - regex sort, where $1 is what gets sorted on, like
        /<!--stuff-->(.+?)<!--end of stuff-->/
        use a qr if you need to use switches, like
        sort_code => {
          0 => 'ra',
        sort_method_regex => {
          qr/<!--stuff-->(.+?)<!--end of stuff-->/i,
        sort_method_regex is a hash ref contain where the key is the column and the value is the regex
    l - an instance of the regex type, where this regex qr@<a\s+href[^>]+?>(.+?)</a>@i attempts to match a link,
        if you wanted to match the href, you would have to use the appropriate regex

  the second digit possibilities are
    a - ascending
    d - decending

  defaults - the beginning default is 'aa', which is an alphabetical ascending sort, I keep this default if I find a value
             in the respective column that contains something that is not "a number", defined by this regex
             /[^0-9.\-+ ]/.  
             If I find a value in the respective column that is only "a number", defined by this regex /^[0-9.\-+]+$/, I use
             'na', which is a numerical ascending sort

             Note that the defaults are problematic, in that I have to look through values, performing regexes.  I stop
             as soon as I can, which in my experience is usually after just a value or two, but if this is not acceptable,
             or if you would like to perform searches in a way contrary to the default, you need to set a value yourself

  dates - initially I started to write different sort for each date format, but found it much better to do something like
          <!--1009411647-->December 26, and then just do a 'aa'.  The epoch time in the date will do a nice ascii sort,
          and not appear in any html.  If this is not acceptable, you can always use a code_ref and sort however you like.
          You likely want to sprintf out to ten digits just so old nine digit stuff will ascii sort properly

sort_method_regex => used in conjunction with sort_method of type regex (see above) sort_column => a zero based, column delimited, list of columns you would like to sort on, where a - means to reverse the sort for example, '0' means to sort on the zeroeth column '3,-1,-0', means to try and sort on the third column, then (if the values from both columns are equal), sort on first column in reverse order, then (if the values all above colulmns are equal), sort on the zeroeth column in reverse order,




Earl Cahill <>


Thanks to Paul T Seamons <> for the idea of a nice, simple two letter code for sort definitions, 'aa', 'nd' and the like. It made it pretty easy to add the regex type. It was also Paul's idea to use <!--1234567890--> for time sorts, which saved oh, so many headaches.