1

I have a bunch of numbers. Let's use these as an example: 7, 18, 24, 53, 75, 15

When I use rsort() on my array, it orders it something like this:

  • 75
  • 7
  • 53
  • 24
  • 18

However, this is not what I want. I want my sorting algorithm to sort my array in numeric order descending, so that it looks like this:

  • 75
  • 53
  • 24
  • 18
  • 15

What sorting algorithm is the right one? I've tried a few but none have done the trick.

5
  • Is it a number or string array. Commented Jan 19, 2015 at 21:16
  • I'm pretty sure it is a number array Commented Jan 19, 2015 at 21:17
  • Give us a var_dump of the starting array itself! Commented Jan 19, 2015 at 21:18
  • There's your answer. The sorting method uses their Ascii value, which is not the same as their numerical value. You would have to convert them first. Commented Jan 19, 2015 at 21:18
  • convert them to what? Commented Jan 19, 2015 at 21:20

1 Answer 1

7

Use SORT_NUMERIC flag:

rsort($myArray, SORT_NUMERIC) 

Without flags, rsort (as well as sort) sorts items without changing types, i.e. strings are compared lexicographically.

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

1 Comment

I will give it the green tick when I can in 7 minutes haha

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.