0

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"]
]
  • More demonstrations: https://stackoverflow.com/a/59175345/2943403 , https://stackoverflow.com/a/44309755/2943403 , https://stackoverflow.com/a/59923150/2943403 – mickmackusa Sep 21 '21 at 01:57
  • By the way, your flaw is here: `foreach($testData as $data) { $gamesPlayed[]=$data[0][0]; $score[]=$data[0][1]; }` You keep accessing the (first and second offset) of the first element as you iterate the rows. I recommend that you remove the leading `[0]` when accessing `$data` . – mickmackusa Sep 21 '21 at 01:58

1 Answers1

-1

you can reduce your problem to one state, for the first scenario, let's just calculate how much score foreach match played (total score/match played) and the inverse for the other scenario. This may be a very simple solution that I find usefull in some cases. but to be more accurate. I guess making it a bitmasking problem is great! let's take the first scenario.

You will keep the first bits for Matchs played and the second ones for the score. for example : Anoir : 4 games : 12 points Mark : 4 games : 11 points let's move to Bits presentation :

Anoir : 0100 1100 
Mark : 0100 0111 

and by a simple comparison : it's clear Anoir will be ranked above Mark I hope that was helpful .

  • That's very clear but I don't know how to calc bits. I just want to sort simple such as Amazon: Lowest Price to Highest Price + Highest Rating to Lowest Rating. – EXECUTOR brandon Sep 21 '21 at 00:21
  • Translating this into bit notation is unnecessary convolution. PHP is very well equipped to deal with the 2-rule requirements and offers two functions to fully satisfy the brief. – mickmackusa Sep 21 '21 at 01:48