#17307 [Opn->Csd]: sorting using usort() does not produce correct result anymore
| From: | joey@php.net | Date: | Tue, 03 Apr 2007 06:58:06 +0000 |
| Subject: | #17307 [Opn->Csd]: sorting using usort() does not produce correct result anymore | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-111036@lists.php.net to get a copy of this message | ||
ID: 17307
Updated by: joey@php.net
Reported By: fcartegnie at nordnet dot fr
-Status: Open
+Status: Closed
Bug Type: Arrays related
Operating System: Linux
PHP Version: 4.4.5
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
As is documented at php.net/usort:
Note: If two members compare as equal, their order in the sorted array
is undefined. Up to PHP 4.0.6 the user defined functions would keep
the original order for those elements, but with the new sort algorithm
introduced with 4.1.0 this is no longer the case as there is no
solution to do so in an efficient way.
Previous Comments:
------------------------------------------------------------------------
[2007-02-19 18:50:08] fcartegnie at nordnet dot fr
Still broken on 4.4.5...
Still using homemade usort routine for 4 years now.
The usort functions really needs to be fixed !
Before usort()<br>
Value: E<br>
Value: C<br>
Value: B<br>
Value: D<br>
Value: A<br>
<p>After usort()<br>
Value: A<br>
Value: D<br>
Value: B<br>
Value: C<br>
Value: E<br>
------------------------------------------------------------------------
[2002-06-26 08:12:55] david dot derr at milesmedia dot com
Hi all. I think this is still a bug as I can partially recreate the
behaviour that fcartegnie@nordnet.fr reported.
On a script that uses a function passed to usort that always returns 0
(implying equal values), I am seeing that the order of elements in the
array is being modified. The only reason I consider this a bug is
because that was not the case in the earlier version I tested. Here are
the details:
-The code I used on both systems:
<?
//The function usort will use. Returns 0 implying that every
comparison is equal.
function fake_sort($left, $right){
return 0;
}
//create our aTest array and add some data to it
$aTest = array();
$aTest[] = "E";
$aTest[] = "C";
$aTest[] = "B";
$aTest[] = "D";
$aTest[] = "A";
//output the order of elements in the array before running usort on
it.
reset($aTest);
echo "Before usort()<br>\n";
while(list($key, $val) = each($aTest)){
echo "Value: ".$val."<br>\n";
}
//sort it
usort($aTest, "fake_sort");
//output the order of elements in the array after running usort on it.
reset($aTest);
echo "<p>After usort()<br>\n";
while(list($key, $val) = each($aTest)){
echo "Value: ".$val."<br>\n";
}
?>
-Incorrect (unexpected) Results
System:
RH 7.2, Apache 1.3.26, PHP 4.2.1
Output:
Before usort()
Value: E
Value: C
Value: B
Value: D
Value: A
After usort()
Value: A
Value: D
Value: B
Value: C
Value: E
-Correct (expected) Results
System:
RH 7.2, Apache 1.3.26, PHP 4.1.2
Output:
Before usort()
Value: E
Value: C
Value: B
Value: D
Value: A
After usort()
Value: E
Value: C
Value: B
Value: D
Value: A
------------------------------------------------------------------------
[2002-06-17 06:52:12] timebreaker at newmail dot ru
Hi!
It's really a problem. I tried to write simple ascending sort routine
and got strange results too. Met the conditions at Intel
P-133/Win98/Apache2.0.35/PPH4.2.0 and PHP4.2.1, it doesn't matter what
the version is. In 4.0.6/4.1.x everything works...
------------------------------------------------------------------------
[2002-06-04 13:58:40] mfischer@php.net
Heh, ok. Still better then a bug in PHP :) thx.
------------------------------------------------------------------------
[2002-06-04 13:44:21] mauf at franzoni dot info
I fixed the problem in my sort routine... it works now (for 4.2.1, so
far it seems it was 4.0.6 the buggy one as it has worked for a long
while). So far there may be at most a backward compatibility issue (I
still have to check back in 4.0.6) but it is no longer worth the
critical status for sure...
sorry for the bothering,
cheers, mauro
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/17307
--
Edit this bug report at http://bugs.php.net/?id=17307&edit=1