Re: [RFC] Change behaviour of array sort functions to return a copy of the sorted array
| From: | Morgan | Date: | Mon, 21 Oct 2024 23:43:37 +0000 |
| Subject: | Re: [RFC] Change behaviour of array sort functions to return a copy of the sorted array | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125830@lists.php.net to get a copy of this message | ||
On 2024-10-22 00:17, mickmackusa wrote:
On Mon, 21 Oct 2024, 18:09 Morgan, <weedpacket@varteg.nz <mailto:weedpacket@varteg.nz>> wrote:
You can’t use:
$sorted_datasets = array_map(sort(...), $datasets);
You want
$sorted_datasets = $datasets;
array_walk($sorted_datasets, sort(...));
A warning: no one should ever use array_walk($sorted_datasets, sort(...)); as general-use script.
When sorting a 2d array in this fashion (only non-fatally executed with numeric first level keys https://3v4l.org/HaU42 <https://3v4l.org/ HaU42>), the first level keys will be used as the sorting flag while sorting each row. This means that different rows may have different sorting flags applied -- effectively corrupting the result. https://3v4l.org/FeIpj <https://3v4l.org/FeIpj> -- notice how rows with keys 2, 5, and 10 are sorted as strings.
Mick
Right, but the issue was that array_walk() is the only callback-parameterised array_* function which is even _compatible_ with sort(). As you point out, even then it's problematic.