sort($array) problem

Posted on 2004-11-22
Last Modified: 2010-05-18
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.

Question by:Vampireofdarkness
    LVL 3

    Expert Comment

    $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");

    LVL 9

    Expert Comment

    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];

    LVL 4

    Accepted Solution

    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"

    LVL 9

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now