Bug #74672 [Opn->Nab]: natsort() not ordering as expected

From: Date: Thu, 01 Jun 2017 18:23:31 +0000
Subject: Bug #74672 [Opn->Nab]: natsort() not ordering as expected
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-209347@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74672&edit=1 ID: 74672 Updated by: danack@php.net Reported by: php-bugs-2014-08 at dotancohen dot com Summary: natsort() not ordering as expected -Status: Open +Status: Not a bug Type: Bug Package: Class/Object related Operating System: Linux melancholy 3.13.0-34-gener PHP Version: 7.1.5 Block user comment: N Private report: N New Comment: The sorting appears to sort runs of digits, rather than just pure strings. "14.6" is less than "14.19" as 6 is less than 19, and the rest of the string is the same. "img0120.jpg" is less than "img1.png" as it is comparing the first 0 to 1 in the second string, as the run of digits in shorter in the second string. > This function implements a sort algorithm that orders alphanumeric strings in the way a human > being would Although it might not be ordering as you would expect, or possibly hope, it is working in a way that makes a reasonably amount of sense. When you look at it from a certain angle. While squinting. As the code has been like that for 14 years, changing the behaviour would not be appropriate, so closing as not a bug. Feel free to add those examples to the documentation, to avoid other people being surprised. Previous Comments: ------------------------------------------------------------------------ [2017-05-30 07:04:07] php-bugs-2014-08 at dotancohen dot com Note that the issue is visible when decimals or 0 padding is used in the numeric part of the strings to be sorted. ------------------------------------------------------------------------ [2017-05-30 06:53:16] php-bugs-2014-08 at dotancohen dot com Description: ------------ The order of elements in the array sorted by natsort() is not as expected by the function's name and description. Test script: --------------- <?php $a = array('13.59', '14.6', '14.19'); natsort($a); var_dump($a); $b = array('img0120.jpg','img0.png', 'img0012.png', 'img10.png', 'img2.png', 'img1.png', 'img3.png'); natsort($b); var_dump($b); Expected result: ---------------- array(3) { [0]=> string(5) "13.59" [1]=> string(5) "14.19" [2]=> string(4) "14.6" } array(7) { [0]=> string(8) "img0.png" [1]=> string(8) "img1.png" [2]=> string(8) "img2.png" [3]=> string(8) "img3.png" [4]=> string(9) "img10.png" [5]=> string(11) "img0012.png" [6]=> string(11) "img0120.jpg" } Actual result: -------------- array(3) { [0]=> string(5) "13.59" [1]=> string(4) "14.6" [2]=> string(5) "14.19" } array(7) { [1]=> string(8) "img0.png" [2]=> string(11) "img0012.png" [0]=> string(11) "img0120.jpg" [5]=> string(8) "img1.png" [4]=> string(8) "img2.png" [6]=> string(8) "img3.png" [3]=> string(9) "img10.png" } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74672&edit=1

« previous php.bugs (#209347) next »