1

I have a php array with mixed types like this :

  $length = array (15, 3, 5, '-');

and I want to sort it to obtain a list of values sorted like this : '-', 3, 5, 15

How can I do that ?

Thanks a lot.

2 Answers 2

2

Well, When using various mixed type in an array , you would need to set your own "priorities" of whats first and whats last.

You could use PHP's usort function to define your own function to sort the array and define your own priorities for each data type or value.

Shai.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, in my case the first item should be alwas '-' and then a normal numbers sort (low to high).
Ok , so just check if your array contains a dash, if yes remove it from the array, sort the array using the regular sort() and than push the dash in the beginning of the array. Shouldn't be complicated at all.
In that case, normal sort() function will give you the desired result
I wrote a big long example for no need, as Vikk correctly said, the regular sort() would do just fine :)
-1

You have to use usort, because PHP's sort function is not stable. See http://quaxio.com/wtf/php.html#turtle17

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.