Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    19
    Thanks
    6
    Thanked 0 Times in 0 Posts

    sort a multidimensional array in a specific order

    I am been trying to get this to work all day... I am the below array

    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    0] => TN 
                
    [1] => Darrel
            
    )
        [
    1] => Array
            (
                [
    0] => TN 
                
    [1] => Anderson
            
    )
        [
    2] => Array
            (
                [
    0] => GA 
                
    [1] => Smith
            
    )
        [
    3] => Array
            (
                [
    0] => NY 
                
    [1] => Porter
            
    )
        [
    4] => Array
            (
                [
    0] => MO 
                
    [1] => Mclean
            
    )
        [
    5] => Array
            (
                [
    0] => NY 
                
    [1] => Johnson
            
    )


    I world like to sort it in this order NY,GA,MO,TN and then in each of those order by alphabetize


    this is my current code.. i might be heading the wrong way with it...

    PHP Code:
    $sortorder = array('NY''GA''MO''TN');


    // compare function 
    function cmpi($a

        
    global 
    $sortorder;
        
    $cmpa array_search($a[0], $sortorder);

      return (
    $cmpa) ? : -1;



    // do the array sorting 
    usort($latest_array'cmpi'); 

    any ideas would be great...

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Your comparator is missing half its arguments O.o
    PHP Code:
    function cmpi($a$b)  
    {  
        global 
    $sortorder;
        
    $aVal array_search($a[0], $sortorder);    
        
    $bVal array_search($b[0], $sortorder);
        
        
    $result $aVal $bVal;
        if (
    $result === 0)
        {
            
    $result strcmp($aVal[1], $bVal[1]);
        }
        return 
    $result;

    That should be what you want.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •