Using the information in the link for Coanda as a starting point, it is clear that PHP using type juggling when comparing objects of different types. The problem is understanding what is getting casted to what during comparison, and I still havn't been able to find a complete list, but in general:
string compared to int will turn into
(int) string <,>,=, (int) int
In this case the string, after casting becomes 0, so all ints are greater than a string. (expected). This is the only case we need to worry about for this sort.
PHP uses quicksort, and most likely chooses its pivot point as array[n/2] where n is the number elements in the array. Knowing these two pieces of information we quicksort the above array:
$pivot = $array[n/2] //n is the number of elements, this sets $pivot='img2'
//compare each element in the list (i am going to this by hand for demonstration)
(int) 'img2' < 2 //int to int comparison;'img2' resolves to 0 and 0 < 2
(int) 'img2' < 1 //int to int comparison;'img2' resolves to 0 and 0 < 1
'img2' > 'img1' // string to string comparison; strcmp is +256
'img2' > 'img 10' //string to string comparison; strcmp is +256
(float) 'img2' < 1.5 //float to float comparison;'img2' resolves to 0 and 0<1.5
'img2' > '3.14' //string to string comparison; strcmp is +54
'img2' > '2.72' //string to string comparison; strcmp is +55
We now have to two new arrays (one for greater and one for less than).
$greater = array('img1', 'img10', '3.14', '2.72);
$less = array(2, 1, 1.5);
Now there is little need to go into further detail as we have accidentally made 2 arrays that have all easily comparable objects in it. $greater has only strings, and we can assume sort will work normally here and considering everything a string.
sort($greater);
var_dump($greater);
produces
array(4) {
[0]=>
string(5) "2.72"
[1]=>
string(4) "3.14"
[2]=>
string(4) "img1"
[3]=>
string(5) "img10"
}
This is what we expected and also the result above. We do the same with $lesser
$lesser = array(2, 1, 1.5);
sort($lesser);
var_dump($lesser);
we get
array(3) {
[0]=>
int(1)
[1]=>
float(1.5)
[2]=>
int(2)
}
Which is also expected. Now when we concat all three arrays together (for the sake of recursiveness I am calling 'img2' an array). We get the results above.
Array
(
[0] => 2.72
[1] => 3.14
[2] => img1
[3] => img10
[4] => img2
[5] => 1
[6] => 1.5
[7] => 2
)
To prove this point, you can follow the same process for this same array, but switch out the $arr[3] with an integer.
$arr = array("img2", 1, "img1", 2, "img10", 1.5, "3.14", "2.72");
sort($arr);
var_dump($arr)
gives you a totally different result because the pivot changed from a string to an int causing the float strings to evaluate as floats.
array(8) {
[0]=>
string(4) "img1"
[1]=>
string(5) "img10"
[2]=>
string(4) "img2"
[3]=>
int(1)
[4]=>
float(1.5)
[5]=>
int(2)
[6]=>
string(4) "2.72"
[7]=>
string(4) "3.14"
}
SORT_NUMERICflag php.net/sortSORT_REGULAR. What does SORT_REGULAR mean? How sort regular assign priority to the items in the sort result?