Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

How do I sort a multidimensional arrays?

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


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;
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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [1nickt]: heh, I hab\ve been. Trying to understand it.
    [1nickt]: perl -e 'print 1.0' ... output '1'.
    [Lady_Aleena]: You could quote it. <c>perl -e 'print "1.0"'>/c> returns 1.0
    [Lady_Aleena]: perl -e 'my $var = "1.0"; print $var;' if it is in a variable also returns 1.0, though perl -e 'my $var = 1.0; print $var;' returns 1.
    [1nickt]: In my case I can simply pass sprintf '%.1f', 1.0 (to Types::Standard:: Int), but what if you didn;t know the precision of the number you were working with? Seems I must be missing something. Oh well, my test list is complete, mooving on ...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (11)
    As of 2017-05-24 18:47 GMT
    Find Nodes?
      Voting Booth?