Req #40888 [Opn->Dup]: Sorts should be stable
| From: | cmb@php.net | Date: | Mon, 12 Mar 2018 12:21:44 +0000 |
| Subject: | Req #40888 [Opn->Dup]: Sorts should be stable | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-214297@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=40888&edit=1
ID: 40888
Updated by: cmb@php.net
Reported by: jo at durchholz dot org
Summary: Sorts should be stable
-Status: Open
+Status: Duplicate
Type: Feature/Change Request
Package: Arrays related
Operating System: Irrelevant
PHP Version: 4.4.5
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Duplicate of request #53341.
Previous Comments:
------------------------------------------------------------------------
[2009-10-22 20:25:07] mac at macnewbold dot com
I don't see any feedback about this very old bug. It is clearly possible to have a stable sort
available in PHP, and clearly more efficient to offer it in-core rather than at the user level. Is
this simply a WONTFIX?
Philosophically, I strongly believe that the developer should be the one to decide whether they want
to use a less efficient stable sort or a more efficient but unstable sort on a case by case basis,
not the language designers. As it stands, we're all forced to use much less efficient solutions
when we require a stable sort.
------------------------------------------------------------------------
[2007-03-22 08:00:37] jo at durchholz dot org
Description:
------------
The manual says this on sorting:
"If two members compare as equal, their order in the sorted array is undefined. Up to PHP 4.0.6
the user defined functions would keep the original order for those elements, but with the new sort
algorithm introduced with 4.1.0 this is no longer the case as there is no solution to do so in an
efficient way."
Bug #18299 requests that the old ("stable") sort behavior be restored. This is denied on
the grounds that there is no efficient implementation for that.
Now it *is* possible to stabilize any sort algorithm, by comparing key positions if values compare
equal.
Um, well, I can imagine that it's inefficient to determine the position of a key. In that case,
I'd propose to introduce a SORT_STABLE flag and have the engine create a key=>position array
when sorting with SORT_STABLE.
That would still be more efficient than trying to stabilize the sort at the PHP level. There,
I'd have to do the following to get a stable sort:
* Make a copy of the array,
wrapping each value in an array ($position, $value)
* Sort with a user-defined function. Have that function compare
$values, and if these compare equal, compare $positions.
* Unwrap the $values from the resulting sorted array.
In effect, this means the engine will have to construct and garbage collect an array for each array
element, so this is horribly inefficient compared to any in-engine solution...
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=40888&edit=1