%sort_priority = ( qr|^/etc/init.d| => 0, qr|^/root| => 1, qr|^/usr/bin| => 2, qr|.?| => 3 ); @new_array = sort { # Get the matching keys ($aKey) = grep { $a->{'path'} =~ $_ } keys %sort_priority; ($bKey) = grep { $b->{'path'} =~ $_ } keys %sort_priority; # and now their priorities $aPri = $sort_priority{$aKey}; $bPri = $sort_priority{$bKey}; # Now compare based on priority then value ($aPri == $bPri ? $a->{'path'} cmp $b->{'path'} : $aPri <=> $bPri) } @array;