G'day soblanc,

The general solution for this is to do a primary sort and then, when primary elements are the same, do a secondary sort. The primary and secondary sorts are separated by a || operator. A typical application would be to sort on "lastname" then, for those with the same "lastname", sort on "firstname".

With the data you've presented, the aN values are already sorted within tN values; there's no way to use that to show how this works.

I ran three tests:

I manually changed @matrix for each run. Here's the final code.

#!/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"; } }

Here's the output for the three runs:

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

Armed with the sorted indices, I'll assume you can create sorted matrices. If you encounter problems with this, show us what you tried and where you encountered difficulties — we can provide further help when we know what problem you're having.

— Ken


In reply to Re: Sort a matrix by row by kcott
in thread Sort a matrix by row by soblanc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.