0

So I want to sort a collection (that's ok for me) but if in this sort result 2 values are equal, I want to compare two other values to determine what goes first.

Ex: I have 30 teams, they all have points, but some teams can have the same number of points. If so, I want to check the victories each team has and put the team with most victories in front of the one with the less.

rankings = $team_stats->sortByDesc(function($product) { return ($product['nhl_season_w'] * 2) + $product['nhl_season_otl'] });

So yeah! That sorts the team's points, now I want to sort the equalities by victories.

Thanks for the help!

EDIT SOLVED:

I use $key + 1 to make sure my team ID's stays the same so it's easier to use to get anything else from the team ranking :)

enter image description here

ettdro
  • 340
  • 4
  • 15
  • Possible duplicate of [What is the syntax for sorting an Eloquent collection by multiple columns?](https://stackoverflow.com/questions/25451019/what-is-the-syntax-for-sorting-an-eloquent-collection-by-multiple-columns) – Jenne Jun 07 '17 at 14:23
  • Possible :/ I'll try to look closely later today thanks!! – ettdro Jun 07 '17 at 14:29
  • Could you not do this at the database level using two order by conditions? – fubar Jun 07 '17 at 21:50
  • I used @sjayswal answer and it worked perfectly :) – ettdro Jun 09 '17 at 02:24

2 Answers2

2

Just concatenate the victories after the point value

$rankings = $team_stats->sortByDesc(function($product) {
     return (($product['nhl_season_w'] * 2) + $product['nhl_season_otl']) . '_' . $procuct['victories'];
});

This will add the victories to the sort string and makes sure the highest victory stays on top when two products have the same score.

Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • I'll try this tonight and give you feedback thanks!! – ettdro Jun 07 '17 at 14:28
  • 1
    Don't forget to add something none numeric in between otherwise `11 3` would be the same as `1 13` (both being 113 concatenated). – Jenne Jun 07 '17 at 14:32
  • Never thought of that!! Haha thanks guys. That's true, Points + victories = a unique number for each team. That's great :) – ettdro Jun 07 '17 at 15:07
  • I just think about it... What if both teams has the same number of victories and points? I'll have to check another column like Goals For, but if a lower team has more Goals For it will not work :/ – ettdro Jun 07 '17 at 16:54
  • Since you're now converting this to a string, and doing a string comparison between them, this doesn't work if the strings compare differently than the numbers would. "1_100" would sort before "1_2" – Prisoner Jun 09 '17 at 03:03
1

Create a multi dimension array, with points being the the first key and victory being the second key, php will automatically sort them key wise.

<?php 
//each array with (Teams, Points, Wins)
$rank[] = array('Team' => "A", 'Points' => 24, 'Wins' => 11);
$rank[] = array('Team' => "B", 'Points' => 26, 'Wins' => 11);
$rank[] = array('Team' => "C", 'Points' => 25, 'Wins' => 10);
$rank[] = array('Team' => "D", 'Points' => 24, 'Wins' => 12);
$rank[] = array('Team' => "E", 'Points' => 25, 'Wins' => 11);
$rank[] = array('Team' => "F", 'Points' => 27, 'Wins' => 13);

foreach ($rank as $key => $row) {
    $points[$key]  = $row['Points'];
    $wins[$key] = $row['Wins'];
}

array_multisort($points, SORT_DESC, $wins, SORT_DESC, $rank);

echo "<ol>";
for ($line = 0; $line < 6; $line++){
    echo "<li><b> Rank</b>";
    foreach($rank[$line] as $key => $value){
        echo "|".$key."-".$value."|";
    }
    echo "</li>";
}    
echo "</ol>";
?>
sjayswal
  • 36
  • 3
  • I never thought of doing it like that!! That's brilliant :) I'll give it a try later and give you feedback. – ettdro Jun 07 '17 at 14:29
  • Just tried and it works like a charm!! :) Thanks! I just didn't use the `echo"
      "` part cause I juste wanted to build the rankings in a Controller to then use it in my view. But the PHP logic is perfect!
    – ettdro Jun 09 '17 at 02:23