SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting an array

    Hello,

    I can't seem to find the right function in the manual for what I need. The closest seems to be asort().

    I have an array that is looks like this:
    Code:
    Array
    (
        [0] => Array
            (
                [uid] => 11523240
                [name] => Paul Wells
            )
    
        [1] => Array
            (
                [uid] => 58006931
                [name] => Alexander Graves
            )
    
        [2] => Array
            (
                [uid] => 58011201
                [name] => Beverley Bunker
            )
    
        [3] => Array
            (
                [uid] => 58012668
                [name] => Matthew Molnar
            )
    
        [4] => Array
            (
                [uid] => 58012787
                [name] => Caitlin Wheeler
            )
    )
    What I want to do is sort it in alphabetical order according to the name. How would I do this?
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,426
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    You will probably need to write a custom function to do this.

  3. #3
    SitePoint Addict Robert_2006's Avatar
    Join Date
    Jun 2006
    Location
    Tarpon Springs Fl.
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's some bits and pieces. Look into array_multisort()


    PHP Code:
               foreach($lists as $key => $value)
                      {
                           
    $id[$key]            = $value['id'];
                           
    $title[$key]         = $value['title'];
                           
    $description[$key]   = $value['description'];
                           
    $tags[$key]          = $value['tags'];
                           
    $image[$key]         = $value['image'];
                           
    $options[$key]       = $value['options'];
                           
    $showprivate[$key]   = $value['showprivate'];
                           
    $displayorder[$key]  = $value['displayorder'];
                           
    $lastpost[$key]      = $value['lastpost'];
                           
    $lastposter[$key]    = $value['lastposter'];
                           
    $lastpostid[$key]    = $value['lastpostid'];
                           
    $lastthread[$key]    = $value['lastthread'];
                           
    $lastthreadid[$key]  = $value['lastthreadid'];
                           
    $lastactivity[$key]  = $value['lastactivity'];
                           
    $lastmember[$key]    = $value['lastmember'];
                           
    $threadcount[$key]   = $value['threadcount'];
                           
    $inviteonly[$key]    = $value['inviteonly'];
                           
    $approved[$key]      = $value['approved'];
                           
    $datecreated[$key]   = $value['datecreated'];
                           
    $createdby[$key]     = $value['createdby'];
                           
    $membercount[$key]   = $value['membercount'];
                        }
      
    $order_types = array('SORT_DESC','SORT_ASC');

      if(!
    in_array($direction$order_types))
        {
          
    $direction 'SORT_DESC';
        }


                switch(
    $sortby)
                {
                  case 
    'membercount';
                    
    array_multisort($membercountconstant($direction),SORT_STRING $idconstant($direction),SORT_STRING $titleconstant($direction),SORT_STRING ,$lists);
                 break;
                 case 
    'title';
                    
    array_multisort($titleconstant($direction),SORT_STRING $idconstant($direction),SORT_STRING $titleconstant($direction),SORT_STRING ,$lists);
                 break;
                 case 
    'description';
                    
    array_multisort($descriptionconstant($direction),SORT_STRING $idconstant($direction),SORT_STRING $titleconstant($direction),SORT_STRING ,$lists);
                 break;

                 case 
    'datecreated';
                    
    array_multisort($datecreatedconstant($direction),SORT_STRING $idconstant($direction),SORT_STRING $titleconstant($direction),SORT_STRING ,$lists);
                 break;

                 } 

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function compare($x, &y) {
        if ( 
    $x[1] == $y[1] )
            return 
    0;
        else if ( 
    $x[1] < $y[1] )
            return -
    1;
        else
            return 
    1;
    }

    usort($Array_to_sort'compare' 
    The online documentation for this can be found @
    http://us.php.net/usort
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    SitePoint Member maksimovic's Avatar
    Join Date
    Jul 2006
    Location
    Novi Sad, Serbia
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Function which will sort your array by one of the keys (from CakePHP):

    PHP Code:
    $SortedArrayByString sortByKey($array"name","asc",SORT_STRING);

    /**
     * Sorts given $array by key $sortby.
     *
     * @param array $array
     * @param string $sortby
     * @param string $order Sort order asc/desc (ascending or descending).
     * @param integer $type
     * @return mixed
     */
        
    function sortByKey(&$array$sortby$order='asc'$type=SORT_NUMERIC
        {
            if (!
    is_array($array))
                return 
    null;

            foreach (
    $array as $key => $val)
            {
                
    $sa[$key] = $val[$sortby];
            }

            
    $order == 'asc'
                
    asort($sa$type)
                : 
    arsort($sa$type);

            foreach (
    $sa as $key=>$val)
            {
                
    $out[] = $array[$key];
            }

            return 
    $out;
        }

    Pay attention to the last parameter, because if you're sorting by string values then you need to pass the correct SORT_STRING flag (SORT_NUMERIC for int/float values)


Bookmarks

Posting Permissions

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