in reply to Re: sorting the column of array element
in thread sorting the column of array element
However, I am guessing you have an array of arrayrefs.
...
sort { $b->[0] <=> $a->[0] } @array
Assuming, like you said, an array of arrayrefs—which is a good assumption, I think—this code would try to order the array refs in @array by the first elements contained in the arrays they reference.
That seems very unlikely to be what he wants.
Update:
If he wants to sort the array referenced by $array[0], the code I gave will work. If he wants to sort all of his "columns," he should sort the indexes based on his sorting column using something like:
And then use the array of sorted indexes to map the sorted order onto the actual columns with something like this:my @indexes = sort {$array[0]->[$b] <=> $array[0]->[$a]} (0..$#{$array +[0]});
my @sort = map { my $a = $_; [ map { $t->[$_] } @indexes ] } @array;
It's up to him to be sure the "columns" are the same length, of course.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: sorting the column of array element
by anonym (Acolyte) on May 24, 2012 at 12:16 UTC | |
by sauoq (Abbot) on May 24, 2012 at 12:27 UTC |