Sorting a Multidimisional Array by Both Score and User Name

Viewed 39

I have an array which has users names and scores.

I'm sorting it by highest score first. It works just fine, but those who have the same score I want them to be sorted by name, and the rest who have 0 have to be also sorted by name.

I'm using this function to sort by r_nb_today (which is the score):

function sortByNbToday($a, $b) {
    $a = $a['r_nb_today'];
    $b = $b['r_nb_today'];
    if ($a == $b) return 0;
    return ($a > $b) ? -1 : 1;
}
usort($disp_user_arr, 'sortByNbToday');

And here is the array output:

Array
(
    [0] => Array
        (
            [u_id] => 9
            [u_name] => Souhaila S.
            [r_nb_today] => 7
        )

    [1] => Array
        (
            [u_id] => 59
            [u_name] => Cirine E.
            [r_nb_today] => 2
        )

    [2] => Array
        (
            [u_id] => 64
            [u_name] => Cyrine B.
            [r_nb_today] => 1
        )

    [3] => Array
        (
            [u_id] => 8
            [u_name] => Jihen B. R.
            [r_nb_today] => 1
        )

    [4] => Array
        (
            [u_id] => 18
            [u_name] => Otail J.
            [r_nb_today] => 0
        )

    [5] => Array
        (
            [u_id] => 15
            [u_name] => Rim H.
            [r_nb_today] => 0
        )

    [6] => Array
        (
            [u_id] => 12
            [u_name] => Bassem D.
            [r_nb_today] => 0
        )

    [7] => Array
        (
            [u_id] => 75
            [u_name] => Mariem N.
            [r_nb_today] => 0
        )

    [8] => Array
        (
            [u_id] => 66
            [u_name] => Khaoula K.
            [r_nb_today] => 0
        )

    [9] => Array
        (
            [u_id] => 74
            [u_name] => Ghada J.
            [r_nb_today] => 0
        )

    [10] => Array
        (
            [u_id] => 62
            [u_name] => Alaeddine M.
            [r_nb_today] => 0
        )

    [11] => Array
        (
            [u_id] => 79
            [u_name] => Khaled B.
            [r_nb_today] => 0
        )

    [12] => Array
        (
            [u_id] => 71
            [u_name] => Desiree Y.
            [r_nb_today] => 0
        )
3 Answers

You can use multisort

$r_nb_today = array_column($arr, 'r_nb_today');
$u_name     = array_column($arr, 'u_name');
array_multisort($r_nb_today, SORT_NUMERIC, SORT_DESC,  
$u_name, SORT_NATURAL, SORT_ASC, $arr);

If by name you want to change direction then use sort_desc for $u_name.

Here is details about array_multisort.

Demo 1 & Demo 2. Check which output you want.

change code a bit:-

function sortByNbToday($a, $b) {
    if ($a['r_nb_today'] == $b['r_nb_today']){ 
        return strcmp($a["u_name"], $b["u_name"]); // add string comparison code
    }
    return ($a['r_nb_today'] > $b['r_nb_today']) ? -1 : 1;
}
usort($disp_user_arr, 'sortByNbToday');

Output:- https://3v4l.org/3XQBQ

function sortByNbToday($a, $b) {
    $score1 = $a['r_nb_today'];
    $score2 = $b['r_nb_today'];
    if ($score1 != $score2){ 
     return -1 * ($score1 - $score2); 
    }
    return strcmp($a['u_name'],$b['u_name']);
}

If both scores are different, return their result, else return a result from u_name string comparison.

Related