Edit report at https://bugs.php.net/bug.php?id=63715&edit=1

 ID:                 63715
 User updated by:    shabbir dot bhojani at objectsynergy dot com
 Reported by:        shabbir dot bhojani at objectsynergy dot com
 Summary:            usort not sorting correctly
 Status:             Not a bug
 Type:               Bug
 Package:            Unknown/Other Function
 Operating System:   Windows 7 x64
 PHP Version:        5.3.19
 Block user comment: N
 Private report:     N

 New Comment:

My mistake: "usort() doesn't pay attention to the magnitude of the return 
value.".

For the record, this solved it for me:

$x = array('a', 'b', 'c', 'd', 'e', 'f');
usort($x, $y = 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]) && isset($custom_sort_data[$b])) {
        return $custom_sort_data[$b] - $custom_sort_data[$a];
      }

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

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


Previous Comments:
------------------------------------------------------------------------
[2012-12-07 03:44:57] ahar...@php.net

Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

usort() doesn't pay attention to the magnitude of the return value.

------------------------------------------------------------------------
[2012-12-07 03:38:08] shabbir dot bhojani at objectsynergy dot com

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 this bug report at https://bugs.php.net/bug.php?id=63715&edit=1

Reply via email to