[php-src] Issue #11756: User sort should honor float instead of casting it to int
| From: | mvorisek | Date: | Fri, 21 Jul 2023 09:50:03 +0000 |
| Subject: | [php-src] Issue #11756: User sort should honor float instead of casting it to int | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245006@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11756
Author: mvorisek
### Description
https://3v4l.org/PBPCR
Currently php casts the compare result to int and
$diff !== 0.0 ? ($diff < 0 ? -1 : 1) :
0 is needed to compare float scores correctly.
Code:
```
<?php
$arr = [1, 2, -2];
usort($arr, fn ($a, $b) => $a - $b);
print_r($arr);
$arr = [0.1, 0.2, -0.2];
usort($arr, fn ($a, $b) => $a - $b);
print_r($arr);
```
Current output:
```
Array
(
[0] => -2
[1] => 1
[2] => 2
)
Array
(
[0] => 0.1
[1] => 0.2
[2] => -0.2
)
```
Expected output:
```
Array
(
[0] => -2
[1] => 1
[2] => 2
)
Array
(
[0] => -0.2
[1] => 0.1
[2] => 0.2
)
```