P is for Practical PerlMonks

### How do I sort a multidimensional arrays?

 on May 28, 2000 at 21:42 UTC 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 [download]``` 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; [download]``` 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]; [download]```

• 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.
• 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2018-09-18 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Eventually, "covfefe" will come to mean:

Results (162 votes). Check out past polls.

Notices?