Page 1 of 1
sort the array
Posted: Fri Oct 31, 2008 5:15 am
by swetha
how to sort the contents of an array in ascending and descending order?
Re: sort the array
Posted: Fri Oct 31, 2008 5:22 am
by Kadanis
Re: sort the array
Posted: Sat Nov 01, 2008 1:39 am
by swetha
assume the input is either a string or a number.how do i obtain the output for descending as well as ascending order in one command ?i have used the command asort for ascending and arsort for descending,but this works only for strings.For numbers,this doesnt work.can u pls give me one command which works for strings as well as for numbers. ?kindly help.
Re: sort the array
Posted: Sat Nov 01, 2008 1:56 am
by requinix
sort, rsort, asort, and arsort work for numbers too. You'd know that if you bothered looking at the manual page for any of them.
Re: sort the array
Posted: Sat Nov 01, 2008 2:00 am
by s.dot
Pay attention to the sorting options, SORT_NUMERIC - in particular.
Re: sort the array
Posted: Sat Nov 01, 2008 5:31 am
by swetha
Code: Select all
$str=explode("\n",$str);
sort($str);
The thing is we use the function in the above method for sorting numbers as well as strings.
It works for strings coorectly but not for numbers.
FOR eg:
10
11
1
2
This is the output :
1
10
11
2
Re: sort the array
Posted: Sat Nov 01, 2008 5:44 am
by requinix
scottayy wrote:Pay attention to the sorting options, SORT_NUMERIC - in particular.
Re: sort the array
Posted: Sat Nov 01, 2008 6:44 am
by omniuni
You will want to look into using PHP's natural sorting.
http://us.php.net/natsort