#40334 [Bgs->Opn]: natsort() does not work for some numbers...

From: Date: Mon, 05 Feb 2007 20:18:46 +0000
Subject: #40334 [Bgs->Opn]: natsort() does not work for some numbers...
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-108444@lists.php.net to get a copy of this message
ID: 40334 User updated by: den4b at mail dot ru Reported By: den4b at mail dot ru -Status: Bogus +Status: Open Bug Type: Arrays related Operating System: Windows XP SP2 PHP Version: 5.2.0 New Comment: 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 ) Previous Comments: ------------------------------------------------------------------------ [2007-02-05 09:36:06] tony2001@php.net 1 coming before 0? No, that's definitely not the result expected. 0 will always come before 1. ------------------------------------------------------------------------ [2007-02-03 01:11:28] 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-03 01:05:59] den4b at mail dot ru Re-opening this bug. ------------------------------------------------------------------------ [2007-02-03 01:03:35] 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-02 23:09:25] tony2001@php.net These numbers are called octal. http://www.php.net/manual/en/language.types.integer.php ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/40334 -- Edit this bug report at http://bugs.php.net/?id=40334&edit=1

« previous php.bugs (#108444) next »