php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40334 natsort() does not work for some numbers
Submitted: 2007-02-02 22:00 UTC Modified: 2016-06-13 13:27 UTC
Votes:4
Avg. Score:3.5 ± 0.9
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: den4b at mail dot ru Assigned: cmb (profile)
Status: Closed Package: *General Issues
PHP Version: 5.2.0 OS: Windows XP SP2
Private report: No CVE-ID: None
 [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
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-02 23:09 UTC] tony2001@php.net
These numbers are called octal.
http://www.php.net/manual/en/language.types.integer.php
 [2007-02-03 01:03 UTC] den4b at mail dot ru
They are strings, that's why they are surrounded with ''. If you are still in doubt - try inserting any string infront of every element, you'll still see the very same effect!

The natural-order-aware algorithm that is used for comparing strings has problems. It was addopted from http://sourcefrog.net/projects/natsort/ - I tested it, and it produced the very same bogus results!
 [2007-02-03 01:05 UTC] den4b at mail dot ru
Re-opening this bug.
 [2007-02-03 01:11 UTC] den4b at mail dot ru
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
)
 [2007-02-05 09:36 UTC] tony2001@php.net
1 coming before 0?
No, that's definitely not the result expected.
0 will always come before 1.
 [2007-02-05 20:18 UTC] den4b at mail dot ru
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
)
 [2007-02-05 20:25 UTC] tony2001@php.net
Start with this link http://sourcefrog.net/projects/natsort/
if you don't understand what "natural sort order" means.
 [2007-02-05 20:44 UTC] den4b at mail dot ru
LOL, you are simply amazing, you know that?!!

That's exactly where I pointed you to! And now look at their example: http://sourcefrog.net/projects/natsort/example-out.txt

Especially note these fragments:
---------------
pic100
pic100a
pic120
pic121
pic02000
---------------
x2-g8
x2-y7
x2-y08
x8-y8
---------------

Do "pic02000" and "x2-y08" come first because of the zero?!

Is there anybody else (professional enough) to address this issue, or will I have to argue with you for the rest of the year? ;-)

Some of the implementations listed on that site contain ERRORS! And I'm afraid PHP has adopted the wrong implementation.
 [2007-06-25 10:27 UTC] dennis at d23 dot nl
The text clearly states:

"Leading zeros are not ignored, which tends to give more reasonable results on decimal fractions"

Strangely the example at http://sourcefrog.net/projects/natsort/example-out.txt does not comply with the above statement.

Actual results comply with the statement, so not a bug it seems, more a  feature request. I can see the use for a toggle to ignore leading zeroes.
 [2007-06-25 11:36 UTC] den4b at mail dot ru
Author meant that the leading zeros are not ignored ONLY for fractional parts. For example:

1.001 < 1.002 < 1.010 < 1.02 < 1.1 < 1.3

But in any other cases, leading zeros are ignored. For example:

pic2
pic02
pic02a
pic3

pic100
pic100a
pic120
pic121
pic02000

x2-y7
x2-y08
x8-y8
 [2016-06-13 13:27 UTC] cmb@php.net
-Status: Open +Status: Closed -Type: Feature/Change Request +Type: Bug -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: cmb
 [2016-06-13 13:27 UTC] cmb@php.net
This issue has been already resolved as of PHP 5.2.10, see
<https://3v4l.org/QocSD>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 01:01:32 2024 UTC