@data = numeric_sorter(2,0, @data); &print; # Print the contents of the array sub print { foreach my $record (@data ) { for my $i (0 .. 4) { print $record->[$i] . " "; } print "\n"; } } # First sort using the 3rd element of the array(port) and if it's the same # sort by looking at the 1st element(time) sub numeric_sorter { my ($field_num, $field_num_secondary, @data) = @_; my @sorted_items = sort { if ($a->[$field_num] != $b->[$field_num]) { $a->[$field_num] <=> $b->[$field_num] } else { $a->[$field_num_secondary] <=> $b->[$field_num_secondary] } } @data; return @sorted_items; }