[PHP-BUG] Bug #54571 [NEW]: asort is not stable (mixes keys)

2011-04-19 Thread mighty...@php.net
From: 
Operating system: any 
PHP version:  5.3.6
Package:  Arrays related
Bug Type: Bug
Bug description:asort is not stable (mixes keys)

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:
---
 (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 bug report at http://bugs.php.net/bug.php?id=54571&edit=1
-- 
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=54571&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=54571&r=trysnapshot53
Try a snapshot (trunk):  
http://bugs.php.net/fix.php?id=54571&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=54571&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=54571&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=54571&r=alreadyfixed
Need backtrace:  
http://bugs.php.net/fix.php?id=54571&r=needtrace
Need Reproduce Script:   
http://bugs.php.net/fix.php?id=54571&r=needscript
Try newer version:   
http://bugs.php.net/fix.php?id=54571&r=oldversion
Not developer issue: 
http://bugs.php.net/fix.php?id=54571&r=support
Expected behavior:   
http://bugs.php.net/fix.php?id=54571&r=notwrong
Not enough info: 
http://bugs.php.net/fix.php?id=54571&r=notenoughinfo
Submitted twice: 
http://bugs.php.net/fix.php?id=54571&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=54571&r=globals
PHP 4 support discontinued:  http://bugs.php.net/fix.php?id=54571&r=php4
Daylight Savings:http://bugs.php.net/fix.php?id=54571&r=dst
IIS Stability:   
http://bugs.php.net/fix.php?id=54571&r=isapi
Install GNU Sed: 
http://bugs.php.net/fix.php?id=54571&r=gnused
Floating point limitations:  
http://bugs.php.net/fix.php?id=54571&r=float
No Zend Extensions:  
http://bugs.php.net/fix.php?id=54571&r=nozend
MySQL Configuration Error:   
http://bugs.php.net/fix.php?id=54571&r=mysqlcfg



Bug->Req #54571 [Bgs]: asort is not stable (mixes keys)

2011-04-19 Thread mighty...@php.net
Edit report at http://bugs.php.net/bug.php?id=54571&edit=1

 ID: 54571
 User updated by:[email protected]
 Reported by:[email protected]
 Summary:asort is not stable (mixes keys)
 Status: Bogus
-Type:   Bug
+Type:   Feature/Change Request
 Package:Arrays related
 Operating System:   any
 PHP Version:5.3.6
 Block user comment: N
 Private report: N

 New Comment:

Ok. Since this behavior seems to be documented, but there are other
functions with 

"preserve_key" parameters, i'd like to make this a feature request.


Previous Comments:

[2011-04-20 01:56:45] danvarin at googlemail dot com

Ok. Since this behavior seems to be documented, but there are other
functions with 

"preserve_key" parameters, i'd like to make this a feature request.


[2011-04-20 01:32:36] [email protected]

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

--------
[2011-04-20 00:12:49] [email protected]

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:
---
 (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