|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-02-02 22:00 UTC] den4b at mail dot ru
Description:
------------
natsort() does not work for some numbers...
Reproduce code:
---------------
$a = array('02', '1', '02000', '03');
natsort($a);
print_r($a);
Expected result:
----------------
Array
(
[1] => 1
[0] => 02
[3] => 03
[2] => 02000
)
Actual result:
--------------
Array
(
[0] => 02
[2] => 02000
[3] => 03
[1] => 1
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 14:00:01 2025 UTC |
Reproduce code: --------------- $a = array('a 02', 'a 1', 'a 02000', 'a 03'); natsort($a); print_r($a); Result (Incorrect): -------------- Array ( [0] => a 02 [2] => a 02000 [3] => a 03 [1] => a 1 )OMFG man.. can't you see it?!! Natural order sort algorithm meant to treat NUMBERS as NUMERIC values, thus comparing them NUMERICALLY, NOT AS CHARACTERS (like you are saying). 02 = 2 1 = 1 Does 2 comes before 1 ?!! It gets even better... 02000 = 2000 Does 2000 comes before 1 ?!! I think left alignment misleads you, so I've added an extra space in front of 1. What will you say this time? Reproduce code: --------------------- $a = array('a 02', 'a 1', 'a 02000', 'a 03'); natsort($a); print_r($a); Result (incorrect) --------------------- Array ( [0] => a 02 [2] => a 02000 [3] => a 03 [1] => a 1 ) Expected result: --------------------- Array ( [1] => a 1 [0] => a 02 [3] => a 03 [2] => a 02000 )