php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #14591 uasort mixes array with equal elements
Submitted: 2001-12-18 17:03 UTC Modified: 2002-06-21 16:32 UTC
From: jan at horde dot org Assigned: hholzgra (profile)
Status: Closed Package: Documentation problem
PHP Version: 4.1.0 OS: Linux/Windows
Private report: No CVE-ID: None
 [2001-12-18 17:03 UTC] jan at horde dot org
If you call uasort with a function that always returns 0 (elements equal) you get a totally screwed array returned.

<?php
function mysort($a, $b)
{
    return 0;
}

$a = array('h', 's', 'i', 'c', 'q', 'm');
var_dump($a);
uasort($a, 'mysort');
var_dump($a);
?>

returns:

array(6) {
  [0]=>
  string(1) "h"
  [1]=>
  string(1) "s"
  [2]=>
  string(1) "i"
  [3]=>
  string(1) "c"
  [4]=>
  string(1) "q"
  [5]=>
  string(1) "m"
}
array(6) {
  [1]=>
  string(1) "s"
  [2]=>
  string(1) "i"
  [3]=>
  string(1) "c"
  [4]=>
  string(1) "q"
  [5]=>
  string(1) "m"
  [0]=>
  string(1) "h"
}

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-12-18 19:24 UTC] hholzgra@php.net
and the problem is ...?

by returning all zeros you made perfectly clear
that you don't care about the order of elements
in your array ;)


 [2001-12-19 04:31 UTC] jan at horde dot org
Hm, I'd rather expect that an array keeps untouched if there is no need to sort it.
 [2001-12-19 05:05 UTC] hholzgra@php.net
well, you can't say that no sorting is needed
in advance as uasort() does not know that
your comparison function is going to return
only zeros

i'm not that deep into qsort implementations
and, yes, i would expect qsort not to swap
elements that are considered equal, but only
for performance reasons ...
(sterling might be able to tell you how and
 why zend_qsort does it internaly)

besides that it is totaly ok that you get 
a different element order back as is still
a valid order for your sort criteria

good old "garbage in, garbage out" principle ;)
 [2001-12-19 05:19 UTC] mfischer@php.net
Hartmut, I don't agree with you at all. This is a major BC problem. If case you haven't checked , PHP 4.0.6 and prior version doesn't change the sort order. I'ld like to hear what sterling can tell us about this.

Not bogus, Reopened.
 [2002-03-21 21:14 UTC] eved_hashem at lycos dot com
I just want to report that the example code for the uasort function in the PHP manual does not work and nothing gets sorted.. I've tried for hours and using multiple books to get uasort to work with multidimensional associative arrays and I have had no success...  Am I possibly printing out the arrays incorrectly?? This is what i Used to display the sorted array in the example in the PHP manual:

for ( $row = 0; $row < 3; $row++ )
{
    while (list($key, value) = each ($array[ $row] ) )
    {
          echo "|$value";
    }
     echo "|<BR>";
}

this can be the only possible thing I find that could be wrong.. if my code is not incorrect than this is a bug in the program..
 [2002-06-17 08:24 UTC] hholzgra@php.net
implementation and behaviour have changed 
but the sort result is still correct, 
even if it may be different to what you
get with 4.0.6 or might expect to get
on a first look

there is no easy/efficient way to solve
this issue with the quicksort algorithm
and the documentation says: 

" The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. If two members compare as equal, their order in the sorted array is undefined."

i will add an additional warning regarding BC

changed to "ducomentation problem"

 [2002-06-17 08:36 UTC] hholzgra@php.net
documentation fixed in CVS
 [2002-06-21 16:32 UTC] venaas@php.net
Just thought I should shed some light on this. Quicksort
will often change the order of equal elements, but qsort()
on glibc and maybe others uses mergesort instead of quick-
sort on small datasets, mergesort does not swap equal
elements (I think). So at least on Linux, PHP would usually
preserve the order of equal elements. Now PHP has it's own
qsort() that is pure quicksort. Maybe we should have merge-
sort too?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Sep 08 11:01:29 2024 UTC