php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63715 usort not sorting correctly
Submitted: 2012-12-07 03:38 UTC Modified: 2012-12-07 04:12 UTC
From: shabbir dot bhojani at objectsynergy dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3.19 OS: Windows 7 x64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: shabbir dot bhojani at objectsynergy dot com
New email:
PHP Version: OS:

 

 [2012-12-07 03:38 UTC] 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" }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-07 03:44 UTC] aharvey@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:44 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2012-12-07 04:12 UTC] shabbir dot bhojani at objectsynergy dot com
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);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 17:01:31 2024 UTC