shamuraq Posted June 17, 2009 Share Posted June 17, 2009 Anyone knows the propoer syntax to arranging numbers either in descending or ascending order? Thanx in advance... Quote Link to comment Share on other sites More sharing options...
trq Posted June 17, 2009 Share Posted June 17, 2009 Where are the numbers coming from? Quote Link to comment Share on other sites More sharing options...
shamuraq Posted June 17, 2009 Author Share Posted June 17, 2009 its randomised set of 4 numbers; eg (2799, 2997, 2979, 2797). I need php to arrange these numbers either in ascending or descending... Quote Link to comment Share on other sites More sharing options...
trq Posted June 17, 2009 Share Posted June 17, 2009 Are the numbers in an array or something? Take a look at sort. I fail to see what this question has to do with math. Quote Link to comment Share on other sites More sharing options...
shamuraq Posted June 17, 2009 Author Share Posted June 17, 2009 The numbers are not in array.... each randomised number is a var on its own... $a = rand(1,999); $b = rand(1,999); $c = rand(1,999); $d = rand(1,999); Now i just need to arrange them in either ascending or descending order... Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted June 17, 2009 Share Posted June 17, 2009 The numbers are not in array.... each randomised number is a var on its own... In which case, why aren't you using an array? for ($i = 1; $i <= 4; $i++) { $a[] = rand(1,999); } Then you can use sort($a); As it is, you'll need to do $x = array($a,$b,$c,$d); sort ($x); list ($a,$b,$c,$d) = $x; Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 18, 2009 Share Posted June 18, 2009 I never like sort being a permutable function. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted June 18, 2009 Share Posted June 18, 2009 I never like sort being a permutable function. I'll agree it would be much nicer being able to say $sortedArray = sort(array(3,1,4,1,5,9)); Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 18, 2009 Share Posted June 18, 2009 I never like sort being a permutable function. I'll agree it would be much nicer being able to say $sortedArray = sort(array(3,1,4,1,5,9)); If that's what Ken actually meant (or was he trying to be funny?), then that wouldn't be better IMO. Without looking under the hood, i'm guessing PHP uses quicksort(and indeed it does) and one of the reasons for that will be it's relatively low memory requirements. By creating a copy, that benefit will be removed. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 18, 2009 Share Posted June 18, 2009 Actually I was being serious. I don't like permutable functions. I have to do something like: <?php $e = array(3,1,4,1,5,9); $k = $e; sort($e); You're saying that's better? It's still creating a copy. There are times where I would like the original array. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 19, 2009 Share Posted June 19, 2009 You're saying that's better? It's still creating a copy. There are times where I would like the original array. Yes, i am saying that's better. As you say, there are times when you want the original. But there are times you don't. Sure, it pushes the problem back to the programmer but it still gives the best solution. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted June 19, 2009 Share Posted June 19, 2009 Besides, you could easily create your own function that has the behavior you want: function cpsort(array $array) { sort($array); return $array; } $e = array(3,1,4,1,5,9); $k = cpsort($e); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.