a11 has asked for the wisdom of the Perl Monks concerning the following question:
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] chr3 20 [2] chr1 30 [3] chr3 5 . . . [n] chr5 5
the code I'm trying to use is:[0] [1] [0] chr1 10 [1] chr1 30 [2] chr3 5 [3] chr3 20 . . . [n] chr5 5
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!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 ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting a two-dimensional array by two columns
by derby (Abbot) on Jul 05, 2006 at 22:29 UTC | |
|
Re: Sorting a two-dimensional array by two columns
by GrandFather (Saint) on Jul 05, 2006 at 22:47 UTC | |
by johngg (Canon) on Jul 05, 2006 at 23:14 UTC | |
by GrandFather (Saint) on Jul 05, 2006 at 23:33 UTC | |
by johngg (Canon) on Jul 09, 2006 at 16:46 UTC | |
by Hue-Bond (Priest) on Jul 05, 2006 at 23:21 UTC | |
by a11 (Novice) on Jul 06, 2006 at 08:33 UTC | |
|
Re: Sorting a two-dimensional array by two columns
by duckyd (Hermit) on Jul 05, 2006 at 22:36 UTC |