in reply to Re: Sorting a two-dimensional array by two columns
in thread Sorting a two-dimensional array by two columns

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

Replies are listed 'Best First'.
Re^3: Sorting a two-dimensional array by two columns
by a11 (Novice) on Jul 06, 2006 at 08:33 UTC

    Thanks, guys!

    The reason why I built up @chrnums and @coords first is because there are also such things as chrX and chrY, so the next step would be to include a small "if" within the code that would push, say, 100 to @chrnums in place of "X" and 101 in place of "Y" for chrX and chrY to be put in the list last.

    So I just thought it'd be faster and more readable to do it in two steps.

    From what you are suggesting I understand that it is more efficient to build just one sorting criterion instead of two and then ask Perl to sort the thing as a string - am I right? But then, will Perl understand that, say, chr10, should go after chr5?

    Thanks again!