-3

Possible Duplicate:
PHP - Multiple uasort functions breaks sorting

I have multidimensional array in php with 3 columns.I needs to sort it by "awarded_units" and if two users have same awarded_units(tiebreaker), then one with least selected unit will come first.

user_id awarded_units selected_units

15       5               2
22       5               1
3        4               2
4        4               5
5        4               1

As you see, I had already sorted array on the basis of awarded_units using some multidimensional sort function. Now, I needs to resolve the tiebreaker condition . Since user_id=15 and user_id=22 have same awarded_units so user_id 22 must come first.

Correct order will be

user_id awarded_units selected_units

22       5               1
15       5               2
5        4               1
3        4               2
4        4               5

Kindly tell me how to do this.Thanks

Community
  • 1
  • 1
Ram
  • 287
  • 2
  • 6
  • 20
  • 1
    Did you look at array sorting before posting here? http://php.net/manual/en/array.sorting.php – Peon Oct 26 '12 at 13:53
  • 2
    PS: Are you creating the array yourself or are you receiving it from a DB? – Peon Oct 26 '12 at 13:55
  • Especially see [Example #3 Sorting database results](http://www.php.net/manual/en/function.array-multisort.php#example-4641) in the PHP manual for the [`array_multisort`](http://php.net/array_multisort) entry - if at all you need to solve that within PHP. You have not posted your array, so this might slightly differ. – hakre Oct 26 '12 at 13:58
  • I had tried usort for this, but not able to do that. – Ram Oct 26 '12 at 14:04
  • @PriteshGupta: Well there are multiple duplicates of that. You're invited to search. You can even do try and error so many duplicates that are. That would even work by understanding no single line of PHP. Hint: Sort in the database, it's faster and does not need any PHP code at all. – hakre Oct 26 '12 at 14:09

2 Answers2

1

You can use array_multisort:

$cols = array();
foreach ($multiArray as $key => $value) 
{
    $cols['awarded_units'][$key]  = $value['awarded_units'];
    $cols['selected_units'][$key] = $value['selected_units'];
}
array_multisort($cols['awarded_units'], SORT_DESC, $cols['selected_units'], SORT_ASC, $multiArray);
hakre
  • 193,403
  • 52
  • 435
  • 836
Leon Storey
  • 3,274
  • 2
  • 25
  • 40
0

Use a custom sort function with usort:

usort($data, function($a, $b) {
    if ($a['awarded_units'] == $b['awarded_units'])
    {
        return $b['selected_units'] - $a['selected_units'];
    }

    return $b['awarded_units'] - $a['awarded_units'];
});

You can also use array_multisort for this, but I prefer using usort - more flexibility and better readability.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • But on the other hand `usort` is complicated because you need to write code for the comparison. Often errors happen with that - your answer is a good example for that, too. – hakre Oct 26 '12 at 14:00