Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Sorting of Multidimensional arrays

Posted on 2007-11-26
Medium Priority
897 Views
A have an array of arrays which I want to sort according to the 3rd element of each of the child arrays, how can I do that?
In the code snippet below I would like the output to be:
5,3
3,5
9,9
``````@array1 = (20,10,5,3,-4);
@array2 = (5,4,3,2,1);
@array3 = (10,9,8,7,6);

@array = (\$array1,\$array2,\$array3);

@sorted = sort by_3rd @array;

foreach \$i (0..\$#array)
{
print \$array[\$i][2],",",\$sorted[\$i],"\n";
}

sub by_3rd
{
???????????
}

Desired output:
``````
0
Question by:shayb022800
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 84

Accepted Solution

ozo earned 1440 total points
ID: 20352941
@array1 = (20,10,5,3,-4);
@array2 = (5,4,3,2,1);
@array3 = (10,9,8,7,6);

@array = (\@array1,\@array2,\@array3);
@sorted = sort{\$a->[2] <=> \$b->[2]} @array;
foreach \$i (0..\$#array)
{
print \$array[\$i]->[2],",",\$sorted[\$i]->[2],"\n";
}
0

Author Closing Comment

ID: 31411072
OZO you are indeed a genius, thanks!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
###### Suggested Courses
Course of the Month8 days, 6 hours left to enroll