php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74672 natsort() not ordering as expected
Submitted: 2017-05-30 06:53 UTC Modified: 2017-06-01 18:23 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: php-bugs-2014-08 at dotancohen dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.1.5 OS: Linux melancholy 3.13.0-34-gener
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php-bugs-2014-08 at dotancohen dot com
New email:
PHP Version: OS:

 

 [2017-05-30 06:53 UTC] 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"
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-30 07:04 UTC] php-bugs-2014-08 at dotancohen dot com
-Summary: var_export does not export a parsable string representation of stdClass objects +Summary: natsort() not ordering as expected
 [2017-05-30 07:04 UTC] 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-06-01 18:23 UTC] danack@php.net
-Status: Open +Status: Not a bug
 [2017-06-01 18:23 UTC] danack@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 11:01:27 2024 UTC