in reply to Sort a matrix by row
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:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sort a matrix by row
by soblanc (Acolyte) on Aug 30, 2022 at 16:57 UTC | |
by haukex (Archbishop) on Aug 30, 2022 at 17:21 UTC | |
by soblanc (Acolyte) on Aug 31, 2022 at 13:45 UTC |