Hello everyone I am a bit confused about sorting an array by 2 values but I know I'm getting close however I cannot get my second sort correct. I'll try to explain the below information:
The $testData array has each player's data as followed in sequence: Games played, Score, Name (FYI the score is mostly negative but sometimes in the positive because this is for ranking our golf league)
I attached my php code below I think the issue lies within the SORT flags in the array_multisort but I tried them all. Thanks for anyone's help for this simple looking issue.
<?php
$testData[]=array(8,-3,"Mike");
$testData[]=array(5,-5,"Bonnie");
$testData[]=array(1,-3,"Zack");
$testData[]=array(6,-3,"Tony");
$testData[]=array(2,-3,"Danny");
$testData[]=array(5,-4,"Bruce");
$testData[]=array(2,-5,"Ellen");
$testData[]=array(5,-4,"Christine");
$testData[]=array(4,-3,"Kenny");
$testData[]=array(3,-5,"Jason");
$testData[]=array(7,-7,"Chris");
$testData[]=array(8,-4,"Steve");
$testData[]=array(7,-6,"Joe");
$testData[]=array(2,-4,"Rodger");
$testData[]=array(7,-2,"Clair");
$testData[]=array(1,-2,"Dean");
foreach($testData as $data)
{
$gamesPlayed[]=$data[0][0];
$score[]=$data[0][1];
}
array_multisort($gamesPlayed,SORT_NUMERIC,$score,SORT_NUMERIC,$testData);
rsort($testData);
print_r(json_encode($testData));
?>
I would like to able to sort by two (2) scenarios...
Scenario 1: Sort by most games played to least games played but if the games played is equivalent or ties another player, the lowest (most negative) scoring player will rank above the other players.
[
[8,-4,"Steve"],
[8,-3,"Mike"],
[7,-6,"Joe"],
[7,-2,"Clair"],
[7,-7,"Chris"],
[6,-3,"Tony"],
[5,-5,"Bonnie"],
[5,-4,"Christine"],
[5,-4,"Bruce"],
[4,-3,"Kenny"],
[3,-5,"Jason"],
[2,-5,"Ellen"],
[2,-4,"Rodger"],
[2,-3,"Danny"],
[1,-3,"Zack"],
[1,-2,"Dean"]
]
Scenario 2: Sort by lowest score to highest score but if the score is equivalent or ties another player, the most games played by a player will rank above the other players.
[
[7,-7,"Chris"],
[7,-6,"Joe"],
[5,-5,"Bonnie"],
[3,-5,"Jason"],
[2,-5,"Ellen"],
[8,-4,"Steve"],
[5,-4,"Bruce"],
[5,-4,"Christine"],
[2,-4,"Rodger"],
[8,-3,"Mike"],
[6,-3,"Tony"],
[4,-3,"Kenny"],
[2,-3,"Danny"],
[1,-3,"Zack"],
[7,-2,"Clair"],
[1,-2,"Dean"]
]