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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |