php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #51229 consecutives sorting with usort and uasort
Submitted: 2010-03-07 17:52 UTC Modified: 2016-06-25 14:42 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: kanea at free dot fr Assigned: cmb (profile)
Status: Closed Package: Arrays related
PHP Version: 5.2.13 OS: All
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: kanea at free dot fr
New email:
PHP Version: OS:

 

 [2010-03-07 17:52 UTC] kanea at free dot fr
Description:
------------
on an array with collection of the same object
the object have 2 property a et b
Exemple:
[
  object:{a=3, b=2}
  object:{a=0, b=2}
  object:{a=2, b=3}
  object:{a=1, b=2}


If you make a first sort with the good fonction and usort on property a, you obtain 

[
  object:{a=0, b=2}
  object:{a=1, b=2}
  object:{a=2, b=3}
  object:{a=3, b=2}
]
The result is that you intend

Now if you make a second sort with another function and usort on property b, you obtain
[
  object:{a=3, b=2}
  object:{a=1, b=2}
  object:{a=0, b=2}
  object:{a=2, b=3}
]
The result is not you can wait. I think that's because the read of the array is make in inverse way.

A possible solution is to mark the placed elements. And not move elements not tested before or after them the marked element.

Best Regards



Test script:
---------------
<?php
function compa($a, $b){ if($a['a']>$b['a']) return 1; else return ($a['a']<$b['a'])?-1:0; }
function compb($a, $b){ if($a['b']>$b['b']) return 1; else return ($a['b']<$b['b'])?-1:0; }
$a = array(array('a'=>1, 'b'=>2), array('a'=>2, 'b'=>3), array('a'=>3, 'b'=>2), array('a'=>0, 'b'=>2), array('a'=>4, 'b'=>2));
usort($a, 'compa');
print_r($a);
usort($a, 'compb');
print_r($a);
?>


Expected result:
----------------
Array
(
    [0] => Array
        (
            [a] => 0
            [b] => 2
        )
    [1] => Array
        (
            [a] => 1
            [b] => 2
        )
    [2] => Array
        (
            [a] => 3
            [b] => 2
        )
    [3] => Array
        (
            [a] => 2
            [b] => 3
        )
)


Actual result:
--------------
Array
(
    [0] => Array
        (
            [a] => 3
            [b] => 2
        )

    [1] => Array
        (
            [a] => 1
            [b] => 2
        )

    [2] => Array
        (
            [a] => 0
            [b] => 2
        )

    [3] => Array
        (
            [a] => 2
            [b] => 3
        )

)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-10 16:11 UTC] peter at f-is dot eu
This behavior is documented:
"Note: If two members compare as equal, their order in the sorted array is undefined."

You should adapt your comparison function so it checks on both a AND b, in one function.
 [2010-03-11 14:06 UTC] kanea at free dot fr
Yes i have seen that's note and read the source code to understand how that's function. This seems that the new logic is more efficient.

Maybe, could it be more visible and present in all u*sort, because uksort and uasort have the same behavior and this it not documented (I work with ua).

For the solution, i have already make the change in my code. But perhaps this can be too documented with best practices (one or two afternoon to recode my function) and understand the solution to organize a liste.

Best regards
 [2014-07-13 13:08 UTC] datibbaw@php.net
-Package: Arrays related +Package: Documentation problem
 [2016-06-25 14:42 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=339490
Log: Fix #51229: consecutives sorting with usort and uasort
 [2016-06-25 14:42 UTC] cmb@php.net
-Status: Open +Status: Closed -Type: Feature/Change Request +Type: Documentation Problem -Package: Documentation problem +Package: Arrays related -Assigned To: +Assigned To: cmb
 [2016-06-25 14:42 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.
 [2020-02-07 06:07 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=32952c4ddc71070da5c2e0344ef50f5afef08ae1
Log: Fix #51229: consecutives sorting with usort and uasort
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 21:01:28 2024 UTC