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

From: Date: Sun, 07 Mar 2010 16:52:46 +0000
Subject: Req #51229 [NEW]: consecutives sorting with usort and uasort
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-148360@lists.php.net to get a copy of this message
From:             
Operating system: All
PHP version:      5.2.13
Package:          Arrays related
Bug Type:         Feature/Change Request
Bug description:consecutives sorting with usort and uasort

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 bug report at http://bugs.php.net/bug.php?id=51229&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=51229&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=51229&r=trysnapshot53
Try a snapshot (PHP 6.0):            http://bugs.php.net/fix.php?id=51229&r=trysnapshot60
Fixed in SVN:                        http://bugs.php.net/fix.php?id=51229&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51229&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=51229&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=51229&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=51229&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=51229&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=51229&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=51229&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=51229&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=51229&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=51229&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=51229&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=51229&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=51229&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=51229&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=51229&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=51229&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=51229&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#148360) next »