php usort对象正在返回意外的顺序

时间:2016-09-16 11:38:41

标签: php usort

以下内容应按上升时间排序驱动程序数组:

cell.someButton.layer.cornerRadius = floor(cell.someButton.frame.height/2)

此实际输出类似于:

function sortDrivers($a, $b)
{
  if (floatval($a->fastestLap) === floatval($b->fastestLap)) {
    return 0;
  }
  return floatval($a->fastestLap) < floatval($b->fastestLap) ? -1 : 1;
}

Log::info("Before:");
foreach($drivers as &$driver){
  Log::info($driver->driverCode . " - " . floatval($driver->fastestLap));
}

usort($drivers, "sortDrivers");

$position = 0;
foreach($drivers as &$driver){
  $driver->position = ++$position;
  $driver->save();
}

Log::info("After:");
foreach($drivers as &$driver){
  Log::info($driver->driverCode . " - " . floatval($driver->fastestLap) . " - " . $driver->position);
}

虽然显然有一些趋势是增加单圈时间,但有几位车手出现在错误的位置。

为什么?

1 个答案:

答案 0 :(得分:0)

道歉。我使用的是错误的排序功能,而不是上面发布的功能。