my @array_of_references; for my $line () { my @values = split ' ', $line; push @array_of_references, \@values; #The \ creates a reference. } my @sorted_refs = sort by_my_custom_func @array_of_references; sub by_my_custom_func { if ($a->[0] < $b->[0]) {return -1} # $a should come before $b elsif ($a->[0] > $b->[0]) {return +1} # $a should come after $b #Execution arrives here only if $a->[0] equals #$b->[0]: if ($a->[1] < $b->[1]) {return +1} # $a should come after $b elsif ($a->[1] > $b->[1]) {return -1} # $a should come before $b else {return 0} } for my $arr_ref (@sorted_refs){ print "@$arr_ref" . "\n";} __DATA__ 7 22 12 20 7 15 1 5 7 10 --output:-- 1 5 7 22 7 15 7 10 12 20