my $sample= [ [5,7,2], [9,4,8], [1,6,3] ]; my @widths= map 0+@$_, @$sample; my @sort= sort map @$_, @$sample; $sample= [ map { [ splice @sort, 0, $_ ] } @widths ];