-1

I need to sort an array to make a rank by points but I can't make it work correctly..

function cmp($a, $b)
{
    return strcmp($a->points, $b->points);
}
usort($teamList, "cmp");  
foreach($teamList as $r)
{
    echo $r->name . " " . $r->points. "<br>";
}

I got this with this code: randomteam*-100 randomteam* -12 randomteam* -12.5 randomteam* -15 randomteam* -15.5 randomteam* -15.5

  • Each randomteam is a different team
PakkuDon
  • 1,627
  • 4
  • 22
  • 21
Raphael Jayme
  • 53
  • 1
  • 8

1 Answers1

1

You are comparing your items as strings. In string terms, -12.5 is less than -15 (because 2 is less than 5 and everything up to there is equal.

Instead, just use return $a->points - $b->points;.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592