Jump to content

Sort Array By Values


rascle

Recommended Posts

Hi

 

I have an array (which is an equation) lets say it is

Array ( [0] => 18x [1] => 10x^2 [2] => 17 )

 

I want to order the array in terms of the value after ^, so that the highest values after ^ are grouped first. Therefore the above array I want it sorted to become

Array ( [0] => 10x^2 [1] => 18x [2] => 17 )

Does anyone have any idea how I would sort it? I have tried using php sort() to no avail.

 

Thanks,

 

Rhys

Edited by Zane
Link to comment
Share on other sites

Using the php.net I have come up with the following, but it is probably rubbish!:

 

function sortdtr($a,$B){
$a = explode('x^',$a);
$b = explode('x^',$B);
if($a==$B){ return 0; }
else if($a>$B){ return -1;}
else if($a<$B){ return 1; }

}

usort($dtr, "sortdtr");
echo "<br/>";
print_r($dtr);

Edited by Zane
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.