Req #51229 [Com]: consecutives sorting with usort and uasort

From: Date: Wed, 10 Mar 2010 15:11:35 +0000
Subject: Req #51229 [Com]: consecutives sorting with usort and uasort
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-148473@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=51229&edit=1

 ID:               51229
 Comment by:       peter at f-is dot eu
 Reported by:      kanea at free dot fr
 Summary:          consecutives sorting with usort and uasort
 Status:           Open
 Type:             Feature/Change Request
 Package:          Arrays related
 Operating System: All
 PHP Version:      5.2.13

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2010-03-07 17:52:45] 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
        )

)



------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=51229&edit=1


Thread (3 messages)

« previous php.bugs (#148473) next »