wolfman77 has asked for the wisdom of the Perl Monks concerning the following question:

How do I sort an array of array by the length of the second array in descending form. For example if I have an array of arrays with 4 entries:
my @array = ( ['1', '2', '3', '4'], ['1','2','3','4','5','6'] ['1','2'] ['1','2','3','4','5','6','7','8'] );
The lengths of @{$array[0]} is 4, @{$array[1]} is 6, @{$array[2}] is 2 and @{$array[3]} is 8. I want to sort the array of arrays by the length of the arrays. So that the array now looks like this:
my @sortedarray = ( ['1','2','3','4','5','6','7','8'], ['1','2','3','4','5','6'], ['1', '2', '3', '4'], ['1','2'] );

Replies are listed 'Best First'.
Re: How do I sort an array of arrays by array length
by kcott (Archbishop) on Feb 27, 2014 at 11:32 UTC

    G'day wolfman77,

    Welcome to the monastery.

    In scalar context, @array_name evaluates to the number of elements in @array_name. You can sort your array like this:

    my @sortedarray = sort { @$b <=> @$a } @array;

    -- Ken

      That works great. Many thanks!
Re: How do I sort an array of arrays by array length
by Corion (Patriarch) on Feb 27, 2014 at 11:28 UTC