let's say I wanted to sort this array multiple times, and instead of wasting CPU power, I wanted to record the order to an array instead.

When you say "sort ... multiple times", do you actually mean "print the sorted array multiple times", or something else like "sort it into different orderings (and print any of these orderings at any time)" ? If the former, note that by sorting the array once, it remains in the sorted order -- you'll always see that order when printing the array sequentially.

The latter case is good if you have an interactive process (e.g. a GUI of some sort for viewing the data), and you want the user to be able to change the viewing order at will (e.g. sorted by one or another dimension of the array).

Is something like this what you're trying to do?

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"; } }

In reply to Re: Sorting two dimensional arrays by graff
in thread Sorting two dimensional arrays by toonski

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.