Bug #54571 [Opn->Bgs]: asort is not stable (mixes keys)
| From: | dtajchreber@php.net | Date: | Tue, 19 Apr 2011 23:32:36 +0000 |
| Subject: | Bug #54571 [Opn->Bgs]: asort is not stable (mixes keys) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-160294@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=54571&edit=1
ID: 54571
Updated by: dtajchreber@php.net
Reported by: mightyuhu@php.net
Summary: asort is not stable (mixes keys)
-Status: Open
+Status: Bogus
Type: Bug
Package: Arrays related
Operating System: any
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
Known, documented, and not a bug.
[1] http://www.php.net/manual/en/array.sorting.php
"If any of these sort functions evaluates two members as equal then the
order is
undefined (the sorting is not stable)."
[2] http://lxr.php.net/opengrok/xref/PHP_5_3/ext/standard/array.c#490
[3] http://lxr.php.net/opengrok/xref/PHP_5_3/Zend/zend_qsort.c#56
Previous Comments:
------------------------------------------------------------------------
[2011-04-20 00:12:49] mightyuhu@php.net
Description:
------------
In the manual it sais: "This function sorts an array such that array
indices maintain their correlation with the array elements they are
associated with."
But when we have the same value the order of the keys is swapped. (See
Test Script). This is unexpected and kind of destroys the correlation.
In computer science terms one would say asort is not a stable sorting
function.
Tested with:
- PHP 5.3.3-7+squeeze1 with Suhosin-Patch
- PHP 5.3.6
- PHP 5.3.5
- PHP 5.2.17
Test script:
---------------
<?php
$testarray = array ('1' => (int) 23, '2' => (int) 1, '4' =>
(int) 1);
var_dump($testarray);
asort($testarray);
var_dump($testarray);
//4 and 2 are switched
?>
Expected result:
----------------
array(3) { [1]=> int(23) [2]=> int(1) [4]=> int(1) }
array(3) { [2]=> int(1) [4]=> int(1) [1]=> int(23) }
Actual result:
--------------
array(3) { [1]=> int(23) [2]=> int(1) [4]=> int(1) }
array(3) { [4]=> int(1) [2]=> int(1) [1]=> int(23) }
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54571&edit=1