http://www.perlmonks.org?node_id=15209

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (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];

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I sort a multidimensional arrays?
by reptile (Monk) on May 28, 2000 at 23:59 UTC
    @array = ([8,9,10], [4,5,6], [7,8,9]); @sorted = sort { $a->[1] <=> $b->[1] } @array

    Updated and fixed thanks merlyn.

      In our last episode, we saw:
      @array = [8,9,10], [4,5,6], [7,8,9]; @sorted = sort { $a->[1] <=> $b->[1] } @array
      in which our nearly knowledgable hero is probably now thumping his head on his desk, realizing that he should have written:
      @array = ([8,9,10], [4,5,6], [7,8,9]); @sorted = sort { $a->[1] <=> $b->[1] } @array
      Because the precedence rules say so, otherwise the second and third element are just tossed off to never-never land.

      -- Randal L. Schwartz, Perl hacker

        You're right about that. I wasn't paying attention and just copied the line from the question. DOH.

        72656B636148206C72655020726568746F6E41207473754A

Re: How do I sort a multidimensional arrays?
by chromatic (Archbishop) on Jun 06, 2000 at 03:13 UTC
    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.
Re: How do I sort a multidimensional arrays?
by tlm (Prior) on Mar 15, 2005 at 23:55 UTC
    In answer to the somewhat more difficult question in Re: 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
Re: How do I sort a multidimensional arrays?
by flope (Initiate) on Mar 15, 2005 at 13:51 UTC
    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];
Re: How do I sort a multidimensional arrays?
by Anonymous Monk on Feb 06, 2003 at 16:24 UTC
    thanks

    Originally posted as a Categorized Answer.