From:
Operating system: any
PHP version: 5.3.3
Package: Arrays related
Bug Type: Feature/Change Request
Bug description:Add a stable sorting flag to sort functions (uasort)
Description:
------------
Starting from php 4.1.0 the sorting of arrays is not stable.
With current sort method is not possible to sort two or more times (with
different sort functions) an array with consistent result.
My suggestion is to add a flag to *sort functions to choose the sorting
algorithm.
I'm not a c programmer, but i think that can be changed a behavior of
zend_qsort with some parameters or add a mergesort that can be used if
stable sort is required.
Test script:
---------------
<?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){ // alpha sort
return strcmp($a1["l"],$a2["l"]);
});
usort($a, function($a1, $a2){ // sort odd first
if($a1["n"]%2===0 && $a2["n"]%2!==0){
return -1;
}elseif($a2["n"]%2===0 && $a1["n"]%2!==0){
return 1;
}else{
return 0;
}
});
print_r($a);
Expected result:
----------------
Array
(
[0] => Array
(
[l] => B
[n] => 2
)
[1] => Array
(
[l] => D
[n] => 4
)
[2] => Array
(
[l] => A
[n] => 1
)
[3] => Array
(
[l] => C
[n] => 3
)
[4] => Array
(
[l] => E
[n] => 5
)
)
Actual result:
--------------
Array
(
[0] => Array
(
[l] => D
[n] => 4
)
[1] => Array
(
[l] => B
[n] => 2
)
[2] => Array
(
[l] => E
[n] => 5
)
[3] => Array
(
[l] => C
[n] => 3
)
[4] => Array
(
[l] => A
[n] => 1
)
)
--
Edit bug report at http://bugs.php.net/bug.php?id=53341&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=53341&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=53341&r=trysnapshot53
Try a snapshot (trunk):
http://bugs.php.net/fix.php?id=53341&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=53341&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=53341&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=53341&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=53341&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=53341&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=53341&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=53341&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=53341&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=53341&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=53341&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=53341&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53341&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=53341&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=53341&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=53341&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=53341&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=53341&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=53341&r=mysqlcfg