Jump to content

[SOLVED] Arranging numbers???


shamuraq

Recommended Posts

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;

 

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.