php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76214 uasort different in different vesion
Submitted: 2018-04-12 11:55 UTC Modified: 2018-04-12 12:18 UTC
From: gaozhenan at 126 dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.16 OS:
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: gaozhenan at 126 dot com
New email:
PHP Version: OS:

 

 [2018-04-12 11:55 UTC] gaozhenan at 126 dot com
Description:
------------
uasort's  problem  in different version(5.x, 7.x)

Test script:
---------------
<?php
$arr = array(
    38 => ['id' => 38, 'score' => 3.45],
    34 => ['id' => 34, 'score' => 3.45],
    30 => ['id' => 30, 'score' => 3.45],

);
uasort($arr, function ($row_a, $row_b) {                                                                                                                     
    if ($row_a['score'] == $row_b['score']) {
        return 0;
    }
    return ($row_a['score'] >= $row_b['score']) ? -1 : 1;
});

print_r($arr);
                                                                                                                                                 
 ~/test/uasort.php[1]                                                                                                              
[gaozhenan@sjs_85_168 test]$php55 uasort.php 
Array
(
    [30] => Array
        (
            [id] => 30
            [score] => 3.45
        )

    [34] => Array
        (
            [id] => 34
            [score] => 3.45
        )

    [38] => Array
        (
            [id] => 38
            [score] => 3.45
        )

)
[gaozhenan@sjs_85_168 test]$php71 uasort.php   
Array
(
    [38] => Array
        (
            [id] => 38
            [score] => 3.45
        )

    [34] => Array
        (
            [id] => 34
            [score] => 3.45
        )

    [30] => Array
        (
            [id] => 30
            [score] => 3.45
        )

)

Expected result:
----------------
the result in php 7.x should be equal in 5.x

Actual result:
--------------
just opposite

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-04-12 12:18 UTC] danack@php.net
-Status: Open +Status: Not a bug
 [2018-04-12 12:18 UTC] danack@php.net
For arrays where items have the same value, you can't depend on them being sorted in a repeatable format.

From the manual page http://php.net/manual/en/function.uasort.php. 
Note:
If two members compare as equal, their relative order in the sorted array is undefined.


I'd suggest adding an element to the array before sorting, that stores what position the item was at before the sorting, and then in the comparison function, if the scores are equal, also compare by the initial position for the entries.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 09 02:01:33 2024 UTC