use warnings; use strict; my $aoh = [ { first => 1, second => ['foo', 'bar', 'baz',], }, { first => 2, second => ['foz', 'barz',], }, { first => 2, second => ['frip', 'barn', 'bazurt',], }, { first => 1, second => ['foo',], }, ]; # use a subroutine to pass a custom # sort code block @$aoh = sort by_custom @$aoh; ######## subs ######## sub by_custom{ return ( # sort on first key - numeric ascending ( $a->{first} <=> $b->{first} ) || ( # then by the size of the second key scalar@{$b->{second} } <=> scalar@{ $a->{second} } ) ); }