php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74042 SORT_NATURAL does not work or pass tests
Submitted: 2017-02-04 00:41 UTC Modified: 2017-02-04 11:17 UTC
From: ejrx7753 at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.0.15 OS: OS X Sierra, Ubuntu 16.04
Private report: No CVE-ID: None
 [2017-02-04 00:41 UTC] ejrx7753 at gmail dot com
Description:
------------
A section of "ext/standard/tests/array/sort_basic.phpt" with case insensitive natural sort does not give the expected result.

Tested on OS X Sierra PHP 7.0.13, PHP 7.1.0, and "PHP 7.0.13-0ubuntu0.16.04.1"


The test expects
  [6]=>
  string(7) "Orange3"
  [7]=>
  string(8) "orange20"

But it gives
  string(8) "orange20"
  [7]=>
  string(7) "Orange3"

Obivously expecting "3" before "20" in nat sort. Note also that the sort_basic does not properly test the insensitive case (as there are no exeptional elements with case considered) so that was not tested and assumed also to be not working.

Test script:
---------------
<?php

// associative array containing unsorted string values
$unsorted_strings = array(
	"l" => "lemon", "o" => "orange",
	"O" => "Orange", "O1" => "Orange1", "o2" => "orange2", "O3" => "Orange3", "o20" => "orange20",
	"b" => "banana",
);

echo "\n-- Testing sort() by supplying string array (natural, case insensitive), 'flag' = SORT_NATURAL|SORT_FLAG_CASE --\n";
$temp_array = $unsorted_strings;
sort($temp_array, SORT_NATURAL|SORT_FLAG_CASE);
var_dump( $temp_array);

Expected result:
----------------
  [6]=>
  string(7) "Orange3"
  [7]=>
  string(8) "orange20"

Actual result:
--------------
  string(8) "orange20"
  [7]=>
  string(7) "Orange3"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-02-04 00:42 UTC] ejrx7753 at gmail dot com
-Package: PDO Core +Package: Arrays related
 [2017-02-04 00:42 UTC] ejrx7753 at gmail dot com
(fixed package meta)
 [2017-02-04 00:45 UTC] ejrx7753 at gmail dot com
Correction - does not properly test the "case sensitive" call. Just a side note not central to the report.
 [2017-02-04 10:27 UTC] ejrx7753 at gmail dot com
I just tested the underlying sort function and also get a failure to compare in natural order.

var_dump(strnatcasecmp("orange20","Orange3"));
var_dump(strnatcmp("orange20","orange3"));
var_dump(strcmp("1","2"));

Outputs
int(1)
int(1)
int(-1)

Expected
-1
-1
-1
 [2017-02-04 10:28 UTC] ejrx7753 at gmail dot com
Nevermind that - underlying sort function works correctly.

Expected
-1
-1
-1
 [2017-02-04 10:29 UTC] ejrx7753 at gmail dot com
Expected
1
1
-1
 [2017-02-04 10:55 UTC] ejrx7753 at gmail dot com
-Status: Open +Status: Closed
 [2017-02-04 10:55 UTC] ejrx7753 at gmail dot com
Nevermind. Ignore this report entirely.
 [2017-02-04 11:17 UTC] requinix@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 08:01:28 2024 UTC