Chaos Project

General => Electronic and Computer Section => Programming / Scripting / Web => Topic started by: G_G on May 13, 2010, 06:01:55 pm

Title: [PHP] Sorting numbers from Highest to Lowest (RESOLVED)
Post by: G_G on May 13, 2010, 06:01:55 pm
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.
Title: Re: [PHP] Sorting numbers from Highest to Lowest
Post by: Ryex on May 13, 2010, 10:46:27 pm
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);
Title: Re: [PHP] Sorting numbers from Highest to Lowest
Post by: G_G on May 13, 2010, 11:12:16 pm
Thanks ryex. That solved everything! PHP really gets at me sometimes.
Title: Re: [PHP] Sorting numbers from Highest to Lowest (RESOLVED)
Post by: Ryex on May 14, 2010, 12:12:16 am
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.