i'm trying sort array in php using custom function, it's not working. here (simplified) code i'm using:
class test { public function sortbyvote($a, $b) { $v1 = $a['votecount']; $v2 = $b['votecount']; if ($v1 > $v2) return +1; if ($v1 < $v2) return -1; return 0; } function test() { $temp = array( array("votecount" => 1), array("votecount" => 4), array("votecount" => 9), array("votecount" => 2), array("votecount" => 3) ); uksort($temp, array($this, "sortbyvote")); } }
can see issue is?
uksort()
sorts keys. in example, keys automatically generated numeric keys 0 - 4. think mean sort values using usort()
. or, if you're looking maintain index association still sort values, you're looking uasort()
. in short, sort borked.
Comments
Post a Comment