[PHP] Sorting numbers from Highest to Lowest (RESOLVED)

Started by G_G, May 13, 2010, 06:01:55 pm

Previous topic - Next topic

G_G

I don't like how php sorts their arrays and numbers. Here's an array I have.
$array = array(5, 1000, 100, 8000);
rsort($array); // Sorts from highest to lowest
for ($i = 0; $i < 4; $i++)
{
   echo $array[$i];
}


Outcome:
8000
5
1000
100

Is there anyway to sort it this way?
8000
1000
100
5

I'm having troubles doing it.

Ryex

May 13, 2010, 10:46:27 pm #1 Last Edit: May 13, 2010, 10:49:01 pm by Ryexander
why is it that php treats everything like a string when it comes to the basic functions?
any way.
it is sorting the numbers by the first place value and then sorting conflicts by the second ect.? wow.
try looking for another sort function or look for extra parameters for that one, i'm sure there are some.

edit:
use
natsort($array);
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

G_G

Thanks ryex. That solved everything! PHP really gets at me sometimes.

Ryex

same. it was designed for an entirely different purpose than c# or ruby. the singular purpose of web design. or at-least it has grown into that purpose. as such it has some rather unexpected behavior when compared with other languages.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />