Hi, I need to sort a two dimensional array (@twodarray) that looks something like:
[0] [1] [0] chr1 10 [1] chr3 20 [2] chr1 30 [3] chr3 5 . . . [n] chr5 5
first by chr number (all chr1 go first, then all chr3, then all chr5, etc.) and within that by column 1 in ascending order. that is, the resulting array should look like that:
[0] [1] [0] chr1 10 [1] chr1 30 [2] chr3 5 [3] chr3 20 . . . [n] chr5 5
the code I'm trying to use is:
my @sortedtda; my @chrnums; my @coords; $i=0; for (@twodarray) { push @chrnums, $twodarray[$i][0]=~ (/(\d+)/); push @coords, $twodarray[$i][1]=~ (/(\d+)/); ++$i; } @sortedtda = @twodarray[ sort { $coords[$a] <=> $coords[$b] || $chrnums[$a] <=> $chrnums[$b] } 0..$#twodarray ];
For a small dataset, it seems to work okay, but for real data the result is somewhat random - perhaps my test dataset is just biased. Is there some kind of mistake or the whole thing doesn't make any sense at all? I would really appreciate any help. Thanks in advance!

In reply to Sorting a two-dimensional array by two columns by a11

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.