Jump to content

[SOLVED] sort($xxx);


malikah

Recommended Posts

Hi, one of my arrays isn't being sorted the way I would like. I'm pulling a bunch of numerical results from a mysql database, then using array_unique(), then sorting them:

 

The results from the mysql database are:

 

128, 128, 128, 128, 128, 128, 128, 128, 128, 143, 143, 143, 143, 143, 143, 143, 143, 143, 207, 207, 207, 207, 207, 207, 207, 207, 207, 21, 21, 238, 238, 238, 238, 238, 238, 238, 238, 238, 42, 42, 42, 534, 534, 534, 534, 534, 534, 534, 71, 71, 71, 71, 71, 71, 71, 71, 71, 806, 806, 806, 806, 806, 806, 806, 806, 806, 863, 863, 863, 863, 863, 863, 880, 880, 880, 880, 880, 880, 880, 880, 880, 9, 9, 9, 9, 9, 9, 9, 9, 9, 985, 985, 985, 985, 985, 985, 985, 985, 985,

 

Then I use PHP to unique and sort them:

 

$result = array_unique($qwe);

sort($result);

foreach ($result as $ans){

print_r($ans);

}

 

The output is: 128 143 207 21 238 42 534 71 806 863 880 9 985.

 

As you can see, there's something not quite as expected. - Any ideas?

Link to comment
Share on other sites

Why are you assuming it is an associative array (well if it's from mysql), here's what I tried:

$m = array(128, 128, 128, 128, 128, 128, 128, 128, 128, 143, 143, 143, 143, 143, 143, 143, 143, 143, 207, 207, 207, 207, 207, 207, 207, 207, 207, 21, 21, 238, 238, 238, 238, 238, 238, 238, 238, 238, 42, 42, 42, 534, 534, 534, 534, 534, 534, 534, 71, 71, 71, 71, 71, 71, 71, 71, 71, 806, 806, 806, 806, 806, 806, 806, 806, 806, 863, 863, 863, 863, 863, 863, 880, 880, 880, 880, 880, 880, 880, 880, 880, 9, 9, 9, 9, 9, 9, 9, 9, 9, 985, 985, 985, 985, 985, 985, 985, 985, 985);

$result = array_unique($m);
sort($result);
print_r($result);

 

And this was the result:

Array ( [0] => 9 [1] => 21 [2] => 42 [3] => 71 [4] => 128 [5] => 143 [6] => 207 [7] => 238 [8] => 534 [9] => 806 [10] => 863 [11] => 880 [12] => 985 ) 

Link to comment
Share on other sites

Correct me if I'm wrong...you get your data from a mysql_query statement right? It seemed to me along the way you have implicitly converted the data to a string type hence the result of the sort is based on a string.  Check your code prior to what you have posted? Perhaps on your mysql_fetch loop.

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.