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 #### if ($a < $b) {return -1} elseif ($a > $b) {return +1} else {return 0} #### $a = 1; $b = 2; my $result1 = spaceship($a, $b); print $result1 . "\n"; my $result2 = spaceship($b, $a); print $result2 . "\n"; sub spaceship { my $x = shift; my $y = shift; if ($x < $y) {return -1} elsif ($x > $y) {return +1} else {return 0}; } --output:-- -1 1