Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array using multiple criteria in PHP [duplicate]

I know there are some other topics about sorting with multiple criteria, but they don't fix my problem. Let's say I have this array:

Array
(
    [0] => Array
        (
            [uid] => 1
            [score] => 9
            [endgame] => 2
        )

    [1] => Array
        (
            [uid] => 2
            [score] => 4
            [endgame] => 1
        )

    [2] => Array
        (
            [uid] => 3
            [score] => 4
            [endgame] => 100
        )

    [3] => Array
        (
            [uid] => 4
            [score] => 4
            [endgame] => 70
        )

)

I want to sort it, putting the one with the HIGHEST score on top. On same score, I want the one with the LOWEST endgame number on top. The sorting mechanisme should rank user1 on top, then user2, then 4 and then user3.

I use this sorting mechanisme:

function order_by_score_endgame($a, $b)
{
  if ($a['score'] == $b['score'])
  {
    // score is the same, sort by endgame
    if ($a['endgame'] == $b['endgame']) return 0;
    return $a['endgame'] == 'y' ? -1 : 1;
  }

  // sort the higher score first:
  return $a['score'] < $b['score'] ? 1 : -1;
}
usort($dummy, "order_by_score_endgame");

This gives me the following array:

Array
(
    [0] => Array
        (
            [uid] => 1
            [score] => 9
            [endgame] => 2
        )

    [1] => Array
        (
            [uid] => 3
            [score] => 4
            [endgame] => 100
        )

    [2] => Array
        (
            [uid] => 2
            [score] => 4
            [endgame] => 1
        )

    [3] => Array
        (
            [uid] => 4
            [score] => 4
            [endgame] => 70
        )

)

As you can see, the array isn't sorted properly... Anyone knows what I'm doing wrong? Thanks a lot!

like image 844
binoculars Avatar asked Jul 03 '14 16:07

binoculars


People also ask

How do I sort an array by multiple columns in PHP?

Function 'array_multisort' to the rescue Well for me the easiest way to do this was using the PHP array_multisort function which gives the ability to sort by multiple keys. The function signature is: array_multisort( &$arr1[, sort type [, &$arr2...]]) and it returns true if successful.

Which function sort multiple array at once in PHP?

array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions. Associative (string) keys will be maintained, but numeric keys will be re-indexed.

How do you sort the array in descending order predefine PHP function?

PHP - Sort Functions For Arrays sort() - sort arrays in ascending order. rsort() - sort arrays in descending order. asort() - sort associative arrays in ascending order, according to the value.

How do you sort an array of objects in PHP?

The usort() function is an inbuilt function in PHP which is used to sort the array of elements conditionally with a given comparator function. The usort() function can also be used to sort an array of objects by object field.


1 Answers

Your function should be like this:

function order_by_score_endgame($a, $b) {
    if ($a['score'] == $b['score']) {
        // score is the same, sort by endgame
        if ($a['endgame'] > $b['endgame']) {
            return 1;
        }
    }

    // sort the higher score first:
    return $a['score'] < $b['score'] ? 1 : -1;
}

Try it out. It will give you result like this:

Array
(
[0] => Array
    (
        [uid] => 1
        [score] => 9
        [endgame] => 2
    )

[1] => Array
    (
        [uid] => 2
        [score] => 4
        [endgame] => 1
    )

[2] => Array
    (
        [uid] => 4
        [score] => 4
        [endgame] => 70
    )

[3] => Array
    (
        [uid] => 3
        [score] => 4
        [endgame] => 100
    )

)
like image 164
Ronak Patel Avatar answered Oct 27 '22 12:10

Ronak Patel