sorting - Php multi sort function -
i'm trying sort standings table not score, weekly wins well. in league, overall score (pick ratio) top dog, wins tiebreaker. have 3 people in 2nd, 2 of them 1 win each, , 0. standings page displays them out of order. same thing in 3rd place section, 1 2 wins should on top. (see link)
i see sorting on standings.php page:
$playertotals = sort2d($playertotals, 'score', 'desc');
and calls sorting function functions.php page:
//the following function found @ http://www.codingforums.com/showthread.php?t=71904 function sort2d ($array, $index, $order='asc', $natsort=false, $case_sensitive=false) { if (is_array($array) && count($array) > 0) { foreach(array_keys($array) $key) { $temp[$key]=$array[$key][$index]; } if(!$natsort) { ($order=='asc')? asort($temp) : arsort($temp); } else { ($case_sensitive)? natsort($temp) : natcasesort($temp); if($order!='asc') { $temp=array_reverse($temp,true); } } foreach(array_keys($temp) $key) { (is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key]; } return $sorted; } return $array; }
so, need sort score first, , wins. have tried:
$playertotals = sort2d($playertotals, 'score', 'desc', 'wins', 'desc');
but doesn't work , know function not meant that.
sorry if i'm misunderstanding question, take want sort score , wins, wins have lower priority? if 2 players have same score, sort wins? if so, can this, , can general concept in language:
usort($arrayofplayers, function($a, $b) { if ($a->score == $b->score) { //check if score equal if ($a->wins == $b->wins) { //if score equal, sort wins return 0; } return ($a->wins > $b->wins) ? 1 : -1; } return ($a->score > $b->score) ? 1 : -1; //sort score since not equal }
http://php.net/manual/en/function.usort.php
you didn't specify format of sorting, take it's array
of player
objects wins
, score
properties. please let me know if misunderstood or if not case.
Comments
Post a Comment