3

I have the following array that I need to have sorted from highest score to lowest.

Array
(
    [0] => Array
        (
            [team id] => 5
            [score] => 52
            [fouls] => 0
        )

    [1] => Array
        (
            [team id] => 4
            [score] => 47
            [fouls] => 0
        )

    [2] => Array
        (
            [team id] => 8
            [score] => 46
            [fouls] => 6
        )

    [3] => Array
        (
            [team id] => 1
            [score] => 46
            [fouls] => 5
        )

    [4] => Array
        (
            [team id] => 9
            [score] => 46
            [fouls] => 3
        )

)

The array above has been sorted already with this function:

function sortByOrder($a, $b){
    $Return = $b['score'] - $a['score'];
    return $Return;
}

usort($Array, 'sortByOrder');

Which seems to work fine since the array with the highest score goes up the list.

However, some teams have equal scores but have committed some fouls. How can I adjust the function to also take the fouls into account when a score equals another score? The team with the least fouls should be placed higher.

Looking forward for creative solutions!

Thanks in advance.

1
  • 1
    if($b['score'] == $a['score']) something like this in sortByOrder? Commented May 16, 2012 at 8:57

2 Answers 2

7

You just have to update your function sortByOrder().

Basically:

function sortByOrder($a, $b){
  if ($b['score'] == $a['score'])
    return $a['fouls'] - $b['fouls'];
  else
    return $b['score'] - $a['score'];
}
Sign up to request clarification or add additional context in comments.

4 Comments

What if fouls contain a negative value?
That would be a nonsense, but when comparing two teams with equal score, for example "-3 fouls" would be considered better than "-1 fouls" (or anything else greater)
So how can I solve this? Since some arrays have a negative value in fouls. I need -3 to be worse than -1
Two options: one is using PHP function abs() on foul values when creating your array (if foul values are always negative), before sorting the array. Second option: update sortByOrder() and simply return $b['fouls'] - $a['fouls'] instead of $a['fouls'] - $b['fouls']. I would recommend the 1st option, since a negative amount of fouls looks like a nonsense. However if you can have either negative & positive numbers, only the 2nd option is valid.
0

I imagine you are using usort to sort your array into an order without maintaining the original key associations of entries.

You can further refine your sort function to break ties when your primary sorting are equal by using this:

function sortByOrder($a, $b) {
    if ($a['score'] == $b['score']) {
        return ($a['fouls'] < $b['fouls']) ? -1 : 1;;
    }
    return ($a['score'] < $b['score']) ? -1 : 1;
}

N.B Please also note with your user defined sort method the only two values you need to return in this case are -1 ( a is less than b ) and 1 ( a is more than b ).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.