Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Just another Perl shrine
 
PerlMonks  

How do I sort a multidimensional arrays?

( #15209=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on May 28, 2000 at 21:42 UTC
Q&A  > sorting


Description:

I have an array of arrays which I need to sort according to the value of the second element of the anonymous arrays that are being referenced by the named array. (did that make sense?) How do I do this exactly?
@array = [8,9,10], [4,5,6], [7,8,9]; # This is the array of arrays I need to sort # by second elements. # Magic step goes here.... # I need to end up with an array like @sorted: @sorted = [4,5,6], [7,8,9], [8,9,10];

Answer: How do I sort a multidimensional arrays?
contributed by reptile

@array = ([8,9,10], [4,5,6], [7,8,9]); @sorted = sort { $a->[1] <=> $b->[1] } @array

Updated and fixed thanks merlyn.

Answer: How do I sort a multidimensional arrays?
contributed by chromatic

To expand a bit on reptile's answer, you can provide your own subroutine reference to sort. In the subroutine, $a and $b are provided and localized special variables used for comparing two elements to each other. In the code snippet, they're array references, and the special arrow notation is just a way of getting at the second element of each. See sort for more details.

Answer: How do I sort a multidimensional arrays?
contributed by tlm

In answer to the somewhat more difficult question in Answer: How do I sort a multidimensional arrays?:

If you seek a one-liner answer, I don't know what it is, but if you just want to get the job done, then simply transpose the matrix, apply the earlier solution (for ordering according to the n-th row elements), and then transpose back. I.e., for the general case of a non-square matrix,

sub transpose { map { my $j = $_; [ map $_[$_][$j], 0..$#_ ] } 0..$#{$_[0]}; } my @array = ([8, 9, 10], [6, 5, 4], [7, 8, 9]); my @sorted = transpose sort { $a->[1] <=> $b->[1] } transpose @array;
kj
Answer: How do I sort a multidimensional arrays?
contributed by flope

What about to sort a multidimensional array using the elements of @{$array[1]}?

@array = [8,9,10], [6,5,4], [7,8,9]; @sorted = [10,9,8], [4,5,6], [9,8,7];

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (7)
    As of 2014-04-19 20:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (483 votes), past polls