I want to sort an array that looks like this (to numerical order instead of 1, 10, 11):
Array ( [0] => 1.jpg [1] => 10.jpg [2] => 11.jpg [3] => 1
The array is passed to the sort function by reference, so you don't need to do the assignment. Furthermore, the sort()
function does not return the sorted array; it returns a success or failure flag, which is why you're getting a 1
in the variable (because the sort was successful).
The first line of your code therefore only needs to look like this:
sort($this->pageLinks);
Secondly, the sort()
function will sort in alphabetical order by default. It is possible to get it to sort in numerical sequence by passing SORT_NUMERIC
as a second parameter. Given the way PHP casts strings to integers, this might just work for you in your case, but since your values aren't strictly numbers, you may find that you need to do the conversion manually.
If this is the case, then you will need to use usort()
instead of sort()
, and define the sorting function yourself, where you compare two values and return the sort order. See the manual page for usort() for more info on how this works.
sort() sorts the array in-place. Don't re-assign it.
sort($this->pageLinks);
$this->pageLinks = sort($this->pageLinks);
You should read the manual on sort(), you give it a reference for an array, and it'll work on it. No need to reassign it.
sort($array);
and not
$array = sort($array);