Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: sorting the column of array element

by sauoq (Abbot)
on May 24, 2012 at 11:45 UTC ( #972225=note: print w/ replies, xml ) Need Help??


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:

my @indexes = sort {$array[0]->[$b] <=> $array[0]->[$a]} (0..$#{$array +[0]});
And then use the array of sorted indexes to map the sorted order onto the actual columns with something like this:
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.

-sauoq
"My two cents aren't worth a dime.";


Comment on Re^2: sorting the column of array element
Select or Download Code
Re^3: sorting the column of array element
by anonym (Acolyte) on May 24, 2012 at 12:16 UTC
    To clear, i have to sort the $array[0] which is a column of numbers out of three columns of a file.None of the code above works actually...dunno. Thanks
      None of the code above works actually.

      The code I gave you in my first response works fine for me:

      t$ perl -le 'my @o=([3, 1, 2]);print "@{$o[0]}";my @s=sort {$b <=> $a} + @{$o[0]};print"@s";' 3 1 2 3 2 1

      -sauoq
      "My two cents aren't worth a dime.";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://972225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-07-12 23:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (242 votes), past polls