Skip to content
Advertisement

Sorting alphabetically by Array Value in Multidimensional Indexed Array After Custom Sort (usort) / Sort Array by Two Values

Original Array:

[0] => Array
    (
        [name] => Light Amethyst
        [description] => color
    )

[1] => Array
    (
        [name] => Stone
        [description] => base material
    )

[2] => Array
    (
        [name] => Emerald
        [description] => color
    )

[3] => Array
    (
        [name] => Brass
        [description] => base material
    )

Applied usort($terms, "mysort"); via the following function(s)

function getSortOrder($c) {
    $sortOrder = array(
        "base material",
        "color"
    );
    $pos = array_search($c['description'], $sortOrder);
    return $pos !== false ? $pos : 99999;
}

function mysort($a, $b) {
    if( getSortOrder($a) < getSortOrder($b) ) {
        return -1;
    }elseif( getSortOrder($a) == getSortOrder($b) ) {
        return 0;
    }else {
        return 1;
    }
}

This successfully ordered the array by the $sortOrder array in the getSortOrder function (base material first, and then color)

[0] => Array
    (
        [name] => Stone
        [description] => base material
    )

[1] => Array
    (
        [name] => Brass
        [description] => base material
    )

[2] => Array
    (
        [name] => Light Amethyst
        [description] => color
    )

[3] => Array
    (
        [name] => Emerald
        [description] => color
    )

Now I am trying to sort this new sorted array by name while keeping the previously applied sort order (base material first and then color).

Expected Output:

[0] => Array
    (
        [name] => Brass
        [description] => base material
    )

[1] => Array
    (
        [name] => Stone
        [description] => base material
    )

[2] => Array
    (
        [name] => Emerald
        [description] => color
    )

[3] => Array
    (
        [name] => Light Amethyst
        [description] => color
    )

Normally I could apply a usort function like such:

usort($people,"sort_name");
function sort_name($a,$b)
{
  return $a["name"] > $b["name"];
}

But this is of course messing up the output of the original description sort.

How can I sort first by description as in the functions above, and then proceed to sort by name while keeping the description sort in tact?

Advertisement

Answer

You could use array_multisort to sort your original data in one go to the expected output (sort by description first, then name) like so

$data = [
    ['name' => 'Light Amethyst', 'description' => 'color'],
    ['name' => 'Stone', 'description' => 'base material'],
    ['name' => 'Emerald', 'description' => 'color'],
    ['name' => 'Brass', 'description' => 'base material']
];


array_multisort(array_column($data, 'description'), SORT_ASC, array_column($data, 'name'), SORT_ASC, $data);

$data will now be sorted by description first and name second.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement