sort($array) problem

I have an array that is filled with 58 character numbers


When I attempt to sort() using info from the order is random. Not any numerical order at all.

I have tried




  sort($arr, SORT_NUMERICAL)


  sort($arr) reset($arr)


  sort($arr, SORT_NUMERICAL) reset($arr)




  function cmp($a, $b) {
    if($a==$b) { return 0; }
    return ($a<$b)?-1:1;
  usort($arr, "cmp");


but neither work.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

$arr = array("1","9","7","3","8","6","8","4","2","1","5","9","6","2","5","8","3","2","6","8","9","2","3","4");

worked for me so I am not sure where are differences are I would probably say use the cmp function method and force the type to be an int. The method below also worked fine for me.

$arr = array("1a","9a","7a","3a","8a","6a","8a","4a","2a","1a","5a","9a","6a","2a","5a","8a","3a","2a","6a","8a","9a","2a","3a","4a");

  function cmp($a, $b) {
    if((int)$a==(int)$b) { return 0; }
    return ((int)$a<(int)$b)?-1:1;
  usort($arr, "cmp");

Try this, each time you reload page you get values mixed


srand((float) microtime() * 10000000);
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
// count array length
$x = count($input);
//make rand keys;
$rand_keys = array_rand($input, $x);

foreach ($rand_keys AS $k => $v){
  echo '<br>' . $input[$v];

hmm yes
lets see
SORT_NUMERICAL is not working, strange ..
try using flags SORT_STRING or SORT_REGULAR which both worked for me

$arr[0] = "0000000000000000000000000000000000000000000000000000000010";
$arr[1] = "0000000000000000000000000000000000000000000000000000000000";
$arr[2] = "0000000000000000000000000000000000000000000000000000000001";
sort($arr, SORT_STRING);

array(3) {
  string(58) "0000000000000000000000000000000000000000000000000000000000"
  string(58) "0000000000000000000000000000000000000000000000000000000001"
  string(58) "0000000000000000000000000000000000000000000000000000000010"


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VampireofdarknessAuthor Commented:
The SORT_NUMERICAL wasn't working. But the SORT_STRING does. I didn't think to try it because I didn't want to assume my numbers weren't numerical XD

Thus, bljak gave me the solution and will receive points -- I never did get what gruntar was proving?

Thanks for the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.