php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65090 array_multisort() with SORT_NATURAL | SORT_FLAG_CASE results in incorrect order
Submitted: 2013-06-21 18:50 UTC Modified: 2013-06-22 09:53 UTC
From: rgrove at smugmug dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.16 OS: Linux 2.6.18-194.3.1.el5
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rgrove at smugmug dot com
New email:
PHP Version: OS:

 

 [2013-06-21 18:50 UTC] rgrove at smugmug dot com
Description:
------------
When the SORT_NATURAL | SORT_FLAG_CASE flags are passed to array_multisort(), it 
sorts strings beginning with the letter "a" above strings beginning with "_", even 
though "_" has a lower ordinal value.

natsort() places "_" before "a" as I would expect, and array_multisort() places 
"_" before "a" when given only the SORT_NATURAL flag, without ORing it with 
SORT_FLAG_CASE.

See the test script for details.

Test script:
---------------
<?php
$keys   = ["_", "a"];
$values = ["first", "second"];

// Results in incorrect order.
array_multisort($keys, SORT_NATURAL | SORT_FLAG_CASE, $values, SORT_ASC);
print_r($keys);
print_r($values);

// Results in correct order.
natsort($keys);
print_r($keys);

// Results in correct order.
array_multisort($keys, SORT_NATURAL, $values, SORT_ASC);
print_r($keys);
?>

Expected result:
----------------
I expect "_" to be sorted before "a", just as natsort() does.

Actual result:
--------------
"a" is sorted before "_".

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-22 09:53 UTC] ab@php.net
-Status: Open +Status: Not a bug
 [2013-06-22 09:53 UTC] ab@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

That flag combination is exactly what natcasesort() is supposed to do. That means 
the behavior is totally correct. Unless you don't need case folding, just remove 
SORT_FLAG_CASE.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 13:01:30 2025 UTC