php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70289 Different sort in php7 (and hhvm) compared to php5
Submitted: 2015-08-18 08:08 UTC Modified: 2015-08-30 23:51 UTC
From: kjarli at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Arrays related
PHP Version: 7.0.0beta3 OS: Ubuntu 14.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kjarli at gmail dot com
New email:
PHP Version: OS:

 

 [2015-08-18 08:08 UTC] kjarli at gmail dot com
Description:
------------
I haven't gotten a proper test setup, but I do have the travis builds. I was working on a priority sorting feature and I had added a test to verify sort orders. On php5 it consistently went right, but on php7 and hhvm it was in reversed order. I fixed it by adding a secondary sort (strcmp).

Build #45 (failed):
https://travis-ci.org/iltar/http-bundle/builds/76059069

Build #46 (failed):
https://travis-ci.org/iltar/http-bundle/builds/76061283

Build #47 (fixed):
https://travis-ci.org/iltar/http-bundle/builds/76062321

The test in question:
https://github.com/iltar/http-bundle/blob/feature/property-accessor/test/DependencyInjection/DecorateRouterPassTest.php#L72-L112

It's about the one with a priority of 50, which is reversed as you can see in the failed output. 


Test script:
---------------
https://github.com/iltar/http-bundle/blob/feature/property-accessor/test/DependencyInjection/DecorateRouterPassTest.php#L72-L112

Expected result:
----------------
I expect to see consistent sorting, but that was not the case.

sequence:
app.henk2_50
app.henk_50

Actual result:
--------------
sequence:
app.henk_50
app.henk2_50

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-18 08:14 UTC] cweiske@php.net
I saw the same in the PEAR core unit tests.

This only happens when the elements are equal. Since they are equal, the order of the elements is unspecified and dependent on the internal implementation and the sort algorithm.

Not a bug in my eyes.
 [2015-08-18 08:19 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2015-08-18 08:19 UTC] requinix@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

Sounds like cweiske's right. PHP's sorting has never promised that equal elements will appear in any particular order. Also know that PHP 7 uses a different sorting algorithm for small arrays than it does larger arrays.
 [2015-08-18 08:22 UTC] kjarli at gmail dot com
This report seems related: https://bugs.php.net/bug.php?id=40888

If it's not a bug and indeed related to small arrays in php7, I would rather see a small notice in the upgrade guide than to have people "waste" time on such a trivial "issue" as it's much better to rely on defined behavior by the developer.
 [2015-08-18 10:16 UTC] cmb@php.net
-Status: Feedback +Status: Open
 [2015-08-18 11:31 UTC] cmb@php.net
-Status: Open +Status: Assigned -Package: Testing related +Package: Arrays related -Assigned To: +Assigned To: cmb
 [2015-08-18 11:31 UTC] cmb@php.net
I agree that a note in UPGRADING is appropriate, event though
the documentation (e.g. for usort()) already states:

| If two members compare as equal, their relative order in the
| sorted array is undefined.

See also the related internal discussion
<http://markmail.org/thread/trb3g4o6vqccitpd>.
 [2015-08-18 11:44 UTC] cmb@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=php-src.git;a=commit;h=1b5e87a579080da1452e173981b7d8a6814f15c0
Log: Fix #70289: Different sort in php7 (and hhvm) compared to php5
 [2015-08-18 11:44 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2015-08-29 05:48 UTC] nazar at mokrynskyi dot com
Here is live example with this bug: https://3v4l.org/AErRF
Neither values nor keys are the same.
Here is example:
<?php
$array = array(
    'is' => 'a',
    'b'
);

ksort($array);
var_dump($array);
 [2015-08-30 23:51 UTC] rasmus@php.net
nazar, actually those are the same. You are comparing integer 0 to string "is" there which means PHP will cast "is" to int which is 0. 0==0.
You can prove this to yourself by forcing a string sort:

    ksort($array, SORT_STRING);
 [2015-09-03 18:10 UTC] ab@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=php-src.git;a=commit;h=1b5e87a579080da1452e173981b7d8a6814f15c0
Log: Fix #70289: Different sort in php7 (and hhvm) compared to php5
 [2016-07-20 11:37 UTC] davey@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=php-src.git;a=commit;h=1b5e87a579080da1452e173981b7d8a6814f15c0
Log: Fix #70289: Different sort in php7 (and hhvm) compared to php5
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 15 19:01:34 2024 UTC