Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:03:47+00:00 2026-05-26T10:03:47+00:00

Is it possible to sort a multidimensional array by multiple columns using natural sort

  • 0

Is it possible to sort a multidimensional array by multiple columns using natural sort in PHP? Here is an example. Suppose I have a 2D array of data, e.g.,

$array[1]['Name'] = 'John';
$array[1]['Age'] = '20';
$array[1]['Code'] = 'ABC 12';

$array[2]['Name'] = 'John';
$array[2]['Age'] = '21';
$array[2]['Code'] = 'ABC 1';

$array[3]['Name'] = 'Mary';
$array[3]['Age'] = '20';
$array[3]['Code'] = 'ABC 10';

I want to sort this array by name (ASC), then by age (DESC), and by code (ASC), all will be sorted naturally. Basically that will be array_multisort with natural sort.

I found many solutions about this topic on the web. Unfortunately, they only support sorting by one column, not multiple column.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:03:48+00:00Added an answer on May 26, 2026 at 10:03 am

    I think you have to implement a custom comparison function for this behavior:

    function myCmp($a, $b) {
     $nameCmp = strnatcasecmp($a['Name'], $b['Name']);
     $ageCmp = strnatcasecmp($a['Age'], $b['Age']);
     $codeCmp = strnatcasecmp($a['Code'], $b['Code']);
    
     if ($nameCmp != 0) // Names are not equal
       return($nameCmp);
    
     // Names are equal, let's compare age
    
     if ($ageCmp != 0) // Age is not equal
       return($ageCmp * -1); // Invert it since you want DESC
    
     // Ages are equal, we don't need to compare code, just return the comparison result
       return($codeCmp);
    }
    

    Then you can call usort($array, 'myCmp'); and should get the desired sorting

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some
Possible Duplicate: Sort multidimensional array by multiple keys I want know how I can
Possible Duplicate: Sorting an associative array in PHP How to sort a multidimensional array
Possible Duplicate: Sorting multidimensional array in PHP How can I sort by key in
Possible Duplicate: Sort an array by a child array's value in PHP I have
How is it possible to sort data from multiple tables by relevance? My table
How would you sort a multidimensional array in JavaScript? I have an array full
is it possible to sort div by using the data function? html <div id=gp_21
Is it possible to sort an array with Unicode / UTF-8 characters in PHP
Possible Duplicate: Sort strings and numbers in Ruby I have an array of place

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.