php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #78992 Strange behavior of SORT_REGULAR insufficiently documented
Submitted: 2019-12-18 15:07 UTC Modified: 2019-12-19 19:09 UTC
From: kfiadeg at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Arrays related
PHP Version: 7.3.13 OS: Linux
Private report: No CVE-ID: None
 [2019-12-18 15:07 UTC] kfiadeg at gmail dot com
Description:
------------
I've encountered inconsistency with sorting string arrays. Sorting arrays that have the same set of elements but in different order produce different results when used with `sort()` function (without any flags).

Test script:
---------------
https://pastebin.com/4uKk37by

Expected result:
----------------
If arrays have the same set of elements, performing `sort()` on each of them should give the same order of elements for every array.

Actual result:
--------------
After sorting array elements keep different order.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-18 17:15 UTC] as@php.net
Interesting. The string "8e7" is interpreted differently by `zendi_smart_strcmp`[0] depending on the element it is being compared to. If the other element is numeric, it is interpreted as scientific notation (8*10^7). If the other element is non-numeric, it is interpreted as a string. As a result, the element "8e7" effectively takes on different values for each iteration of the sort algorithm.

Changing this behavior would be a bc break. However it feels worth documenting that `SORT_REGULAR` has counter-intuitive / buggy behavior.

[0] https://github.com/php/php-src/blob/2a76384b87cd8c48c626641ac9e820e426d47de8/Zend/zend_operators.c#L2724-L2773
 [2019-12-19 09:33 UTC] cmb@php.net
-Summary: Inconsistent sorting +Summary: Strange behavior of SORT_REGULAR insufficiently documented -Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2019-12-19 09:33 UTC] cmb@php.net
While there is already a warning regarding this behavior, I think
the warning message and the description of SORT_REGULAR can be
improved.
 [2019-12-19 19:09 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&revision=348593
Log: Fix #78992: Strange behavior of SORT_REGULAR insufficiently documented
 [2019-12-19 19:09 UTC] cmb@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: cmb
 [2019-12-19 19:09 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2019-12-19 19:10 UTC] salathe@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=1a1d1a1ae3e2808a82b937c03fb8f2a75380bafc
Log: Fix #78992: Strange behavior of SORT_REGULAR insufficiently documented
 [2019-12-20 00:35 UTC] mumumu@php.net
Automatic comment from SVN on behalf of mumumu
Revision: http://svn.php.net/viewvc/?view=revision&revision=348615
Log: Fix #78992: Strange behavior of SORT_REGULAR insufficiently documented

Bug: https://bugs.php.net/78992 (Verified) Strange behavior of SORT_REGULAR insufficiently documented
 [2020-02-07 06:04 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=92530c1bc7eb4912dd74e24520ba58630c7006ca
Log: Fix #78992: Strange behavior of SORT_REGULAR insufficiently documented
 [2020-12-30 11:59 UTC] nikic@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=41335771f703218f88cb5132774d3d753a361d01
Log: Fix #78992: Strange behavior of SORT_REGULAR insufficiently documented
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 10:01:26 2024 UTC