usort sorting multiple fields

37,366

Solution 1

How about:

$arr = Array (
    0 => Array (
        'name' => 'Jonah',
        'age' => '27',
    ),
    1 => Array (
        'name' => 'Bianca',
        'age' => '32',
    ),
    2 => Array (
        'name' => 'Jonah',
        'age' => '25',
    ),
    3 => Array (
        'name' => 'Bianca',
        'age' => '35',
    ),
);
function comp($a, $b) {
    if ($a['name'] == $b['name']) {
        return $a['age'] - $b['age'];
    }
    return strcmp($a['name'], $b['name']);
}

usort($arr, 'comp');
print_r($arr);

output:

Array
(
    [0] => Array
        (
            [name] => Bianca
            [age] => 32
        )

    [1] => Array
        (
            [name] => Bianca
            [age] => 35
        )

    [2] => Array
        (
            [name] => Jonah
            [age] => 25
        )

    [3] => Array
        (
            [name] => Jonah
            [age] => 27
        )

)

Solution 2

usort($arr, function($a, $b)
{
    $name = strcmp($a['name'], $b['name']);
    if($name === 0)
    {
        return $a['age'] - $b['age'];
    }
    return $name;
});

Solution 3

How about:

<?php

function getRandomName() {
    $possible = "ab";
    $possible_len = strlen($possible);
    $r = '';
    for ($i = 0; $i < 4; $i++) {
        $r .=  substr($possible, mt_rand(0, $possible_len-1), 1);
    }
    return ucfirst($r);
}

$a = array();
for ($i = 0; $i < 10; $i++) {
    $a[] = array('name' => getRandomName(), 'age' => rand(1,10), 'start_order' => $i);
}
$order = array('name' => 'desc', 'age' => 'asc');

print_r($a);

usort($a, function ($a, $b) use ($order) {
    $t = array(true => -1, false => 1);
    $r = true;
    $k = 1;
    foreach ($order as $key => $value) {
        $k = ($value === 'asc') ? 1 : -1;
        $r = ($a[$key] < $b[$key]);
        if ($a[$key] !== $b[$key]) {
            return $t[$r] * $k;
        }

    }
    return $t[$r] * $k;
});

print_r($a);
Share:
37,366
user1205775
Author by

user1205775

Updated on July 09, 2022

Comments

  • user1205775
    user1205775 almost 2 years

    Is it possible to use usort to sort multiple fields in a multidimensional array? For example, I want to sort name alphabetically and then from those records I want to sort them by age. Is this possible using sort?

    Array ( 
        [0] => Array ( 
            [name] => Jonah 
            [age] => 27 
        )
        [1] => Array (
            [name] => Bianca 
            [age] => 32 
        )
    )
    
  • user1205775
    user1205775 about 12 years
    Out of curiosity, if I were sorting name but it was an integer instead (for example), instead of using strcmp what function would you recommend using?
  • Tim Cooper
    Tim Cooper about 12 years
    @user1205775: Just using simple arithmetic operators would work fine.
  • GRosay
    GRosay over 5 years
    This solution is the best I've found after multiples research ! It works independently on the number of sort parameters you want and in both ascending or descending orders.
  • Dominique
    Dominique almost 5 years
    This definitely is the best, most versatile solution!
  • Swee Hong
    Swee Hong almost 3 years
    Hi, if age want to desc? is it return $a['age'] + $b['age']; ?
  • Toto
    Toto almost 3 years
    @SweeHong: Use: return $b['age'] - $a['age'];
  • somtam
    somtam over 2 years
    if age was a string you can do strcmp($a['age'], $b['age']);