Please see How do I change/delete my post? and use <code> tags to format code, sample input, and expected output. Fixed by GrandFather.

Anyway, here's a pure-Perl solution. It doesn't actually sort the matrix, it just gives you the sorted indicies of the rows, but that's enough to get your expected output, and hopefully you can see how to use the output to reorder the matrix if you like*.

#!/usr/bin/env perl use warnings; use strict; use Data::Dump; my @matrix = ( [qw/ t1 t1 t2 t2 t1 t2 /], [qw/ a1 a2 a1 a2 a3 a3 /], [qw/ mis mis mis mis del del /], ); my @idx = sort { $matrix[0][$a] cmp $matrix[0][$b] or $matrix[1][$a] cmp $matrix[1][$b] } 0..$#{$matrix[0]}; dd @idx; # (0, 1, 4, 2, 3, 5) my @out = map { $matrix[2][$_] } @idx; dd @out; # ("mis", "mis", "del", "mis", "mis", "del")

* Update:


In reply to Re: Sort a matrix by row by haukex
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.