1

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] => 111.jpg [4] => 12.jpg [5] => 12a.jpg [6] => 13.jpg [7] => 14.jpg [8] => 15.jpg [9] => 16.jpg [10] => 2.jpg [11] => 3.jpg [12] => 4.jpg [13] => 5.jpg [14] => 6.jpg [15] => 7.jpg [16] => 8.jpg [17] => 9.jpg )

when i use sort() it just becomes "1" instead of a sorted array.

code:

$this->pageLinks = sort($this->pageLinks);  // the array is a property in a class     
print_r($this->pageLinks); // want to display the sorted array here but it just returns 1
4
  • 1
    sort returns true on success. It applies the sort to the array you pass in. You don't need to do the assignment again. Commented Sep 22, 2011 at 15:22
  • 1
    If you use a function and it does not work like you expect (guessed), you should at first read the manual about the function to understand all the details. Please do so before asking questions here. Commented Sep 22, 2011 at 15:27
  • 1
    @Xeoncross - inconsistency with what? There are many languages that do in-place array sorting, and all of PHP's sorting functions do their sorting in-place. Commented Sep 22, 2011 at 15:28
  • Your desired sort seems ambiguously described in your question. Can you please (for the benefit of all future readers) improve your question to provide an "out-of-order" input array, and your desired output array? Commented Feb 3, 2018 at 15:18

3 Answers 3

10

sort() sorts the array in-place. Don't re-assign it.

Correct:

sort($this->pageLinks);

Incorrect:

$this->pageLinks = sort($this->pageLinks);
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

1

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.