Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP usort() order in case of equality

Tags:

php

sorting

In the PHP manual for usort(), it states:

If two members compare as equal, their relative order in the sorted array is undefined.

Also,

A new sort algorithm was introduced. The cmp_function doesn't keep the original order for elements comparing as equal.

Then my question is: what happens if two elements are equal (e.g. the user defined function returns 0)? I am using this function and apparently equal items are arranged randomly in the sorted array.

like image 287
linkyndy Avatar asked Feb 24 '12 21:02

linkyndy


People also ask

How does Usort work in PHP?

The usort() function in PHP sorts a given array by using a user-defined comparison function. This function is useful in case if we want to sort the array in a new manner. This function assigns new integral keys starting from zero to the elements present in the array and the old keys are lost.

Which PHP function sorts an array in ascending order?

PHP - Sort Functions For Arrayssort() - sort arrays in ascending order. rsort() - sort arrays in descending order. asort() - sort associative arrays in ascending order, according to the value. ksort() - sort associative arrays in ascending order, according to the key.

How do you sort an associative array in PHP?

The arsort() function sorts an associative array in descending order, according to the value. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.

What does Uasort () function returns on failure?

The uasort() function returns true on success or false on failure. The $callback function returns an integer.

How to sort an array using usort in PHP?

PHP - Function usort() - The usort() function sorts an array by a user defined comparison function. This function assigns new keys for the elements in the array. Existing keys will be r

What are the examples of PHP usort ()?

Lets us discuss the examples of PHP usort (). 1) In this example we are sorting an array of an integer using usort () function available in PHP. This is a custom sort and here we are sorting in descending order. In this example, we are trying to sort an array of the string by using the usort () function available in PHP.

How to use the comparison operator in PHP for sorting?

For example, when you use the sort () function to sort an array of numbers, PHP uses the built-in comparison operator to compare the numbers. To specify a custom comparison function for sorting, you use the usort () function: The usort () function has two parameters:

What is comparision_function in usort ()?

2) comparision_function (second param): This is the second parameter that this function takes as an argument and this will be the user-defined function containing the sorting logic with it. We can define one function and call the user-defined function from this usort () function.


2 Answers

Be careful not to confuse "undefined" and "random".

A random implementation would indeed be expected to give a different ordering every time. This would imply there was specific code to shuffle the results when they're found to be equal. This would make the algorithm more complex and slower, and rarely be a desirable outcome.

What undefined means is the opposite: absolutely no care has been taken while designing the algorithm to have predictable or stable order. This means the result may be different every time you run it, if that happens to be the side effect of the algorithm on that data.

You can see the core sort implementation in the PHP source code. It consists of a mixture of "quick sort" (divide and conquer) and insertion sort (a simpler algorithm effective on short lists) with hand-optimised routines for lists of 2, 3, 4, and 5 elements.

So the exact behaviour of equal members will depend on factors like the size of the list, where in the list those equal members come, how many equal members there are in one batch, and so on. In some situations, the algorithm will see that they're identical, and not swap them (the ideal, because swapping takes time); in others, it won't directly compare them until they've already been moved relative to other things, so they'll end up in a different order.

like image 86
IMSoP Avatar answered Oct 27 '22 01:10

IMSoP


I also find the same while sorting my array, then I found some custom made function because php have some limitation to use defined sorting function .

http://php.net/manual/en/function.uasort.php#Vd114535

PHP 7 uses a stable sort algorithm for small arrays (< 16), but for larger arrays the algorithm is still not stable. Furthermore PHP makes no guarantee whether sorting with *sort() is stable or not https://bugs.php.net/bug.php?id=53341.

like image 38
Muneer Alam Avatar answered Oct 27 '22 00:10

Muneer Alam