Solved

Sorting of Multidimensional arrays

Posted on 2007-11-26
2
886 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
[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
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl - Mawk 2 105
Shell script to bind my job to half the number of vCPUs in Solaris 8 123
Search in text file in column and compare 4 66
perl to convert excel to csv 3 304
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

762 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