Sorting a multi-dimensional array

Posted on 2005-04-19
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 84

    Accepted Solution

    @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
    LVL 1

    Author Comment

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

    Expert Comment

    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 Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    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 (, 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, fr…

    733 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

    25 Experts available now in Live!

    Get 1:1 Help Now