php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #24004 Bring back unchanging usort() values if equal
Submitted: 2003-06-03 17:45 UTC Modified: 2014-12-30 10:41 UTC
Votes:5
Avg. Score:3.8 ± 1.6
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:0 (0.0%)
From: tuxedobob at mac dot com Assigned:
Status: No Feedback Package: Arrays related
PHP Version: * OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tuxedobob at mac dot com
New email:
PHP Version: OS:

 

 [2003-06-03 17:45 UTC] 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?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-28 09:13 UTC] 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
 [2014-04-16 18:28 UTC] levim@php.net
-Status: Open +Status: Feedback -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: *
 [2014-04-16 18:28 UTC] levim@php.net
As someone unfamiliar with the PHP 4 behavior, could you describe it again? I think I understand but am not quite sure.
 [2014-04-16 18:29 UTC] levim@php.net
-Package: *General Issues +Package: Arrays related
 [2014-12-30 10:41 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Fri Nov 16 03:01:26 2018 UTC