#!/usr/bin/env perl use strict; use warnings; my @matrix = ( [qw{t1 t1 t2 t2 t1 t2}], [qw{a2 a1 a3 a2 a3 a1}], [qw{mis mis mis mis del del}], ); print "Original\n"; print_matrix(\@matrix); my @sorted_indices = sort { $matrix[0][$a] cmp $matrix[0][$b] || $matrix[1][$a] cmp $matrix[1][$b] } 0 .. $#{$matrix[0]}; print "Sorted indices\n"; print "@sorted_indices\n"; sub print_matrix { my ($matrix) = @_; for my $row (0 .. $#$matrix) { print join(' ', @{$matrix[$row]}), "\n"; } } #### ken@titan ~/tmp $ ./pm_11146491_sort_matrix.pl Original t1 t1 t2 t2 t1 t2 a1 a2 a1 a2 a3 a3 mis mis mis mis del del Sorted indices 0 1 4 2 3 5 ken@titan ~/tmp $ ./pm_11146491_sort_matrix.pl Original t1 t1 t2 t2 t1 t2 a1 a2 a3 a2 a3 a1 mis mis mis mis del del Sorted indices 0 1 4 5 3 2 ken@titan ~/tmp $ ./pm_11146491_sort_matrix.pl Original t1 t1 t2 t2 t1 t2 a2 a1 a3 a2 a3 a1 mis mis mis mis del del Sorted indices 1 0 4 5 3 2