use strict; my @myarray = ( ['one','two','three','four'], ['first','second','third','fourth'], ['jim','bob','bill','sally'], ); my @orders; for my $idx ( 0 .. 3 ) { my @sort = sort { $myarray[$a][$idx] cmp $myarray[$b][$idx] } ( 0 .. 2 ); push @orders, [ @sort ]; } for my $idx ( 0 .. 3 ) { print "\nrows sorted by value of column $idx:\n"; for my $row ( @{$orders[$idx]} ) { print "$myarray[$row][$_] " for ( 0 .. 3 ); print "\n"; } }