From:             shabbir dot bhojani at objectsynergy dot com
Operating system: Windows 7 x64
PHP version:      5.3.19
Package:          Unknown/Other Function
Bug Type:         Bug
Bug description:usort not sorting correctly

Description:
------------
Doesn't sort correctly. I'm attempting to sort so that the values 'c' and
'd' show 
up on the top in that order followed by the other values sorted using
strcasecmp. 
'c' and 'd' do show up on the top of the sorted array but not in that
order.

Test script:
---------------
$x = array('a','b','c','d','e','f');
usort($x, function ($a, $b) {
      // must return an integer less than, equal to, or greater than zero
if the first argument is considered to be respectively less than, equal to,
or greater than the second

      static $custom_sort_data = array(
    'c' => 256,
    'd' => 255,
      );

      if (isset($custom_sort_data[$a]))
        return -$custom_sort_data[$a];
      if (isset($custom_sort_data[$b]))
        return $custom_sort_data[$b];

      return strcasecmp($a, $b);
    });
var_dump($x);

Expected result:
----------------
array(6) { [0] => string(1) "c" [1] => string(1) "d" [2] => string(1) "a"
[3] => 
string(1) "b" [4] => string(1) "e" [5] => string(1) "f" }

Actual result:
--------------
array(6) { [0] => string(1) "d" [1] => string(1) "c" [2] => string(1) "a"
[3] => 
string(1) "b" [4] => string(1) "e" [5] => string(1) "f" }

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

Reply via email to