From:             php at sameprecision dot org
Operating system: irrelevant
PHP version:      5.2.6
PHP Bug Type:     Arrays related
Bug description:  usort assumes comparison operation is transitive

Description:
------------
Using usort, not all pairs of elements are compared.  If a comparison
operation is not transitive, this leads to unexpected ordering, even though
every pair of elements are comparable.
This should probably be mentioned in the documentation.


Reproduce code:
---------------
//goal: sort $array so that an element is not a substring of any
subsequent elements

$array = array('aa','b','a');

//if $a is a substring of $b, return 1.  Else return -1
function compare($a,$b){
   return strpos($b,$a)===false ? -1 : 1;
}


usort($array,'compare');

print_r($array);

Expected result:
----------------
Array ( [0] => aa [1] => b [2] => a )

Actual result:
--------------
Array ( [0] => a [1] => b [2] => aa )


-- 
Edit bug report at http://bugs.php.net/?id=45988&edit=1
-- 
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=45988&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=45988&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=45988&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=45988&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=45988&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=45988&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=45988&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=45988&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=45988&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=45988&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=45988&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=45988&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=45988&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=45988&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=45988&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=45988&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=45988&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=45988&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=45988&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=45988&r=mysqlcfg

Reply via email to