Sorting a multi-dimensional array

Posted on 2005-04-19
Medium Priority
Last Modified: 2012-06-27
My array is made by n rows and 9 columns, and I need to sort the whole array with ref to the 5th column (id=4).


Question by:winmeister
LVL 85

Accepted Solution

ozo earned 375 total points
ID: 13813660
@sorted = sort{$a->[$id] <=> $b->[$id]} @array;  #if the $id'th column is numerically
@sorted = sort{$a->[$id] cmp $b->[$id]} @array;  #if the $id'th column is to be sorted alphabetically

Author Comment

ID: 13864971
the unsorted array is @array, but what are $a and $b?
LVL 20

Expert Comment

ID: 13867015
Perl's sort operator can take a comparison function as one of its parameters, in case you want something other than the default lexical sort. The $a and $b are variables with which the comparison function is called containing the left hand side and the right hand side, respectively. It's a convention left over from very early versions of Perl.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans
Suggested Courses

809 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