The Schwartzian transform is the classic way to do this

To build upon your good reply, and as a personal exercise, I wanted to implement the same using the Guttman-Rosler transform (*), which is a optimization of the Schwartzian Transform based on the fact that the internal sort used by sort is faster than the Perl code you can supply to it. This is my first GRT ever :^). I also replaced the sprintf and pattern matching with a substr:

my @chrnums = ( [qw(chr1 10)], [qw(chr3 20)], [qw(chr1 30)], [qw(chr3 5)], [qw(chr5 5)], ); my @s = map { $chrnums[(substr $_,8)] ## 8 on architectures where a long is 4 byt +es. See Config } sort map { pack "LLA*", (substr $chrnums[$_][0], 3), $chrnums[$_][1], $_ } 0..$#chrnums;

(*) Update: As pointed out, it seems your code is already GRT (or almost). I didn't notice since I didn't read it line by line.

--
David Serrano


In reply to Re^2: Sorting a two-dimensional array by two columns by Hue-Bond
in thread 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.