I have looked at the php documentation, tutorials online and none of them how usort is actually working. I have an example i was playing with below.
$data = array( array('msg' => 'some text','month' => 11,'level' => 10), array('msg' => 'some text','month' => 5,'level' => 10), array('msg' => 'some text','month' => 8,'level' => 10), array('msg' => 'some text','month' => 12,'level' => 10), array('msg' => 'some text','month' => 2,'level' => 10), array('msg' => 'some text','month' => 3,'level' => 10), array('msg' => 'some text','month' => 4,'level' => 10), array('msg' => 'some text','month' => 7,'level' => 10), array('msg' => 'some text','month' => 10,'level' => 10), array('msg' => 'some text','month' => 1,'level' => 10), array('msg' => 'some text','month' => 6,'level' => 10), array('msg' => 'some text','month' => 9,'level' => 10) );
I wanted to be able to sort the months from 12 to 1 (since their unorganized) through some help this was the solution
function cmp($a, $b) { if ($a["month"] == $b["month"]) { return 0; } return ($a["month"] < $b["month"]) ? -1 : 1; } usort($data, "cmp");
but i dont understand how the function cmp sorts the array. i tried printing out each variable $a and $b like this:
function cmp($a, $b) { echo "a: ".$a['month']."<br/>"; echo " b: ".$b['month']."<br/>"; echo "<br/><br/>"; }
and the output was
a: 3 b: 5 a: 9 b: 3 a: 3 b: 8 a: 6 b: 3 a: 3 b: 12 a: 1 b: 3 a: 3 b: 2 a: 10 b: 3 a: 3 b: 11 a: 7 b: 3 a: 4 b: 3 a: 12 b: 2 a: 5 b: 12 a: 12 b: 11 a: 8 b: 12 a: 5 b: 8 a: 2 b: 11 a: 6 b: 9 a: 7 b: 6 a: 6 b: 4 a: 10 b: 6 a: 1 b: 6 a: 9 b: 4 a: 7 b: 1 a: 10 b: 7
it makes no sense to how the sort is working and why cmp($a, $b) is used. i have tried to print out all its processes as you can see but have not come to any solution to how it all works..
thanks
PHP usort() Function $a=array(4,2,8,6); usort($a,"my_sort");
Although there is a basic array sorting function, called simply sort(), it makes no attempt to preserve the values of your keys and so usually does more harm than good. In its place are asort() and ksort(), which are very similar- asort() sorts an array by its values, whereas ksort() sorts an array by its keys.
Approach: The usort() function is an inbuilt function in PHP which is used to sort the array of elements conditionally with a given comparator function. The usort() function can also be used to sort an array of objects by object field.
Sorting functions in PHP are currently unstable, which means that the order of “equal” elements is not guaranteed.
The callback provided to the sorting functions in PHP have three return values:
0: both elements are the same -1 (<0): the first element is smaller than the second 1 (>0): the first element is greater
Now, usort
probably uses some kind of quicksort or mergesort internally. For each comparison it calls your callback with two elements and then decides if it needs to swap them or not.
The function cmp
itself doesn't do the sorting. It just tells usort
if a value is smaller, equal or greater than another value. E.g. if $a = 5
and $b = 9
it will return 1 to indicate that the value in $b
is greater than the one in $a
.
Sorting is done by usort
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With