爲什麼小數沒有適當的排序:爲什麼這個usort()函數不能正確地對小數進行排序?
13
11
14
10
12.5
---------------------------------------------------------
descending order:
14
12.5
13
11
10
與此代碼:
class Customer {
public $score;
public function __construct($score) {
$this->score = $score;
}
}
$customers = [];
$customers[] = new Customer(13);
$customers[] = new Customer(11);
$customers[] = new Customer(14);
$customers[] = new Customer(10);
$customers[] = new Customer(12.5);
if(is_array($customers) && count($customers) > 0)
{
foreach($customers as $customer)
{
echo '<div>'.$customer->score.'</div>';
}
}
echo '<hr/>';
echo '<div>descending order:</div>';
usort($customers, function($a, $b) {
return $b->score - $a->score;
});
if(is_array($customers) && count($customers) > 0)
{
foreach($customers as $customer)
{
echo '<div>'.$customer->score.'</div>';
}
}
http://stackoverflow.com/questions/16520000/usort-issue-with-decimal-numbers – Kivylius