Req #24004 [Opn->Fbk]: Bring back unchanging usort() values if equal

From: Date: Wed, 16 Apr 2014 18:28:53 +0000
Subject: Req #24004 [Opn->Fbk]: Bring back unchanging usort() values if equal
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185292@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=24004&edit=1 ID: 24004 Updated by: levim@php.net Reported by: tuxedobob at mac dot com Summary: Bring back unchanging usort() values if equal -Status: Open +Status: Feedback Type: Feature/Change Request -Package: Feature/Change Request +Package: *General Issues -Operating System: Mac OS X 10.2.6 +Operating System: Any -PHP Version: 4.3.2 +PHP Version: * Block user comment: N Private report: N New Comment: As someone unfamiliar with the PHP 4 behavior, could you describe it again? I think I understand but am not quite sure. Previous Comments: ------------------------------------------------------------------------ [2003-10-28 09:13:09] thochgreve at php-resource dot de the manual states that there is no efficient solution to keep the original order if two elements are equal - I say there is: usort creates an array with numeric indices anyway just do that before the sorting and if the comparison function returns 0, compare the indices ------------------------------------------------------------------------ [2003-06-03 17:45:01] tuxedobob at mac dot com The script: <?php function comp($a, $b) { if (ceil($a) < ceil($b)) return (-1); if (ceil($a) > ceil($b)) return (1); return 0; } $arr = array('5.2', '5.4', '5.5', '5.9'); for ($x = 0; $x < 10; $x++) { usort($arr, "comp"); print_r($arr); } ?> (Any useful function can also be used for comp.) When running the above script, usort() reverses the array upon each iteration. The documentation says as much might happen. However, it's much more useful if it doesn't. Perhaps there can be a new function so usort() can work the way it did in PHP<=4.0.6 for when the speed penalty is not an issue? ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=24004&edit=1

« previous php.bugs (#185292) next »