php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26412 natsort ignores extra white space
Submitted: 2003-11-25 14:02 UTC Modified: 2003-11-25 15:40 UTC
From: phpbug at reuben dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.3 OS: Mac OS X
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
25 + 7 = ?
Subscribe to this entry?

 
 [2003-11-25 14:02 UTC] phpbug at reuben dot com
Description:
------------
natsort (and related functions like strnatcmp) appear to 
collapse all white space chars into just one space, so 
that 'a a' sorts at the same level as 'a  a' (so the 
order of the returned sort is not determined), while it 
should sort one before the other (which one is more 
"natural" is up for discussion, but it shouldn't 
randomly return one or the 
other).

Reproduce code:
---------------
$array = array('a b','a  a','a a','aa','a  b','ab');
natsort($array);
echo "<pre>";
foreach ($array as $val)
	echo "$val\n";
echo "</pre>";

Expected result:
----------------
Should print:

aa
ab
a a
a b
a  a
a  b

-OR-

aa
a a
a  a
ab
a b
a  b

Actual result:
--------------
Actually prints (changes order if you change order in 
array definition):

aa
a a
a  a
ab
a  b
a b

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-25 15:40 UTC] iliaa@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

' ' is 10 and 'b' is 98, make your own conclustions. 
 [2003-11-25 16:22 UTC] jpatrin at pnicorp dot com
If you would actually look at the bug, you'd notice that "a a" id sortes as before "a  a", which is inconsistent with the sorting of "a b" and "a  b".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 23:01:32 2024 UTC