Edit report at https://bugs.php.net/bug.php?id=53341&edit=1
ID: 53341
Updated by: cmb@php.net
Reported by: goetas at lignano dot it
Summary: Add a stable sorting flag to sort functions (uasort)
Status: Open
Type: Feature/Change Request
Package: Arrays related
Operating System: any
PHP Version: 5.3.3
Block user comment: N
Private report: N
New Comment:
> this bug still appears on php 7.0.6, [â¦]
To make it clear: this is not a bug. The fact that PHP doesn't
make any guarantees about the stability of the sort order is a
deliberate design decision, which is clearly documented[1]:
| If any of these sort functions evaluates two members as equal
| then the order is undefined (the sorting is not stable).
[1] <http://php.net/manual/en/array.sorting.php>
Previous Comments:
------------------------------------------------------------------------
[2016-06-14 12:31:04] mdg12v at gmail dot com
this bug still appears on php 7.0.6, it was working right on php 5.6.
magento totals sorting component calculating wrong total because of this bug.
i bet many places affected because of this bug
------------------------------------------------------------------------
[2016-01-29 15:12:15] cw at clement dot hk
@cmb, this is more like a feature request than a bug report.
------------------------------------------------------------------------
[2015-08-13 13:31:12] cmb@php.net
AFAIK PHP 7 uses a stable sort algorithm for small arrays (< 16),
but for larger arrays the algorithm is still not stable.
Furthermore PHP makes no guarantee whether sorting with *sort() is
stable or not.
------------------------------------------------------------------------
[2015-08-13 12:59:01] cw at clement dot hk
Please note that this is fixed since PHP 7, so this issue can be close now.
------------------------------------------------------------------------
[2015-05-08 21:23:05] cmb@php.net
While there might be use cases demanding a stable sort, this is
clearly none of them. Just sort the array with a single usort(),
what is faster, by the way:
<?php
$a = array(
array("l"=>"B", "n"=>2),
array("l"=>"A", "n"=>1),
array("l"=>"C", "n"=>3),
array("l"=>"E", "n"=>5),
array("l"=>"D", "n"=>4),
);
usort($a, function($a1, $a2){ // sort even first
if($a1["n"]%2===0 && $a2["n"]%2!==0){
return -1;
}elseif($a2["n"]%2===0 && $a1["n"]%2!==0){
return 1;
}else{// alpha sort
return strcmp($a1["l"],$a2["l"]);
}
});
print_r($a);
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=53341
--
Edit this bug report at https://bugs.php.net/bug.php?id=53341&edit=1