Why are decimals not properly sorted:
13
11
14
10
12.5
---------------------------------------------------------
descending order:
14
12.5
13
11
10
with this code:
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>';
}
}
Casting decimal 0.5
to integer changes it to 0
. Change your usort function to:
usort($customers, function($a, $b) {
if($b->score - $a->score >= 0){
return 1;
}
return -1;
});
Output:
descending order:
14
13
12.5
11
10
PHP Manual says:
Caution: Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.
try
usort($customers, function($a, $b) {
return strnatcmp($b->score,$a->score);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With