Solved

Sorting of Multidimensional arrays

Posted on 2007-11-26
2
881 Views
Last Modified: 2010-04-21
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:

Open in new window

0
Comment
Question by:shayb022800
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 360 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

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now