views:

25

answers:

4

Hello everyone,

I have an array which contains the following numbers:

10000
900
670
600
500

I want to sort the array in that format above. Largest to smallest, thus using rsort(). However the outcome turns out to be:

900
670
600
500
10000

Looks like rsort() just looks at the first digit of the whole number to sort the array. Is there any fix to this?

Thanks,

Kevin

+3  A: 

It may be that the numbers are actually strings. The simplest thing to do would be to use the SORT_NUMERIC flag.

rsort($array, SORT_NUMERIC);
Yacoby
+1  A: 

Make sure you are calling rsort($arr, SORT_NUMERIC).

Tim Cooper
Thanks, the code works!!
Kevin
+1  A: 

Try including the SORT_NUMERIC flag.

rsort($myArray, SORT_NUMERIC);

http://www.php.net/manual/en/function.sort.php

A: 

Use natsort() to sort naturally, and reverse the result:

natsort($array);
$array = array_reverse($array);
BoltClock