Jump to content

[SOLVED] Arranging numbers???


shamuraq

Recommended Posts

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...

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.