#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Basic array sort. First numbers, then characters


    Hi;

    How can I sort this array so it sorts numbers first then a-z all ascending.

    In other words I want it ot show 1,1,2,a,a,b,l,v

    $array = array(1,2,'m',1,'a','v','b','a','l');
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,997
    Rep Power
    9397
    Mixing numbers and strings with a regular sort can produce unusual results. Specify SORT_STRING.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    867
    Rep Power
    275
    you can also try using THIS

    this got me close (extra comma at end)
    PHP Code:
    $array = array(1,2,'m',1,'a','v','b','a','l');
    natsort($array);
    echo 
    "<pre>";
    foreach(
    $array as $value){
    echo 
    "$value";
    echo 
    ",";
    }
    echo 
    "</pre>"
    gave this output
    Code:
    1,1,2,a,a,b,l,m,v,
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    Originally Posted by DonR
    you can also try using THIS

    this got me close (extra comma at end)
    PHP Code:
    $array = array(1,2,'m',1,'a','v','b','a','l');
    natsort($array);
    echo 
    "<pre>";
    foreach(
    $array as $value){
    echo 
    "$value";
    echo 
    ",";
    }
    echo 
    "</pre>"
    gave this output
    Code:
    1,1,2,a,a,b,l,m,v,
    nice but surely you could use implode so that you dont have to do foreach, plus extra code to remove , at the end
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    How about this: Why can't I get array $e to be sorted?
    PHP Code:
    $data[] = array('volume' => 67'edition' => 2);
    $data[] = array('volume' => 86'edition' => 1);
    $data[] = array('volume' => 85'edition' => 6);
    $data[] = array('volume' => 98'edition' => 2);
    $data[] = array('volume' => 86'edition' => 6);
    $data[] = array('volume' => 67'edition' => 7);

    foreach(
    $data as $key => $row)
        {
            
    //$new_arr_vol = 
            
    $v[] = ($row['volume']);
            
    $e[] = ($row['edition']);
        }
    array_multisort$vSORT_DESC,$eSORT_ASC);


    foreach(
    $v as $val => $row)
        {
            echo 
    $row."\n";    
        }
        
    echo 
    "<hr />";
    foreach(
    $e as $val => $row)
        {
            echo 
    $row."\n";    
        }
    /*
    prints:
    98
    86
    86
    85
    67
    67
    <hr />2
    1
    6
    6
    2
    7
    */ 
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    surely the manual which you linked to in your other thread makes it clear what is happening?

    it is sorting by volume, and then corresponding "value" in second array. so this doesnt sort two arrays INDEPENDENTLY of each other.

    so if you look at your example, it sorted volume i.e. array('volume' => 98, 'edition' => 2);

    hence in the $e 2 comes first..

    why not just use arsort or equiv. function twice to sort volume & edition separately!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    867
    Rep Power
    275
    Originally Posted by paulh1983
    nice but surely you could use implode so that you dont have to do foreach, plus extra code to remove , at the end
    Thanks for the advice..I'm still a newbie when it comes to PHP [and javascript to be honest] and have been following along with most of the advice given in this forum as part of my learning.
    implode() isn't something I've seen much (and I didn't see it listed in the examples for natsort() ), so, thats why I didn't use it. But, that would've been the proper way to go about my example for sure.

    I appreciate all the advice given to me when I "screw up".
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,853
    Rep Power
    6351
    Implode wouldn't be listed in any manual pages for sorting, as it's usually used for output and has nothing to do with sorting. It is, however, the way you get around the "trailing comma" problem. Any time you have a list, use an array and the built-in array-specific functions to manage it (unless there's a good reason not to).

    Comments on this post

    • DonR agrees : thanks for the explanation, Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo