Results 1 to 6 of 6

Thread: array sorting problem

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default array sorting problem

    Any ideas on how to sort the following array so that if it ends in "--DIRECTORY" it will be placed in the beginning of the array and the "--DIRECTORY" listings will also be sorted a-z case insensitive as well. For example

    Code:
    $a=array('that--DIRECTORY','next--DIRECTORY','big','this','dad--DIRECTORY','fruit','apple--DIRECTORY');
    will be sorted to look like:

    Code:
    $a[0]=apple--DIRECTORY
    $a[1]=dad--DIRECTORY
    $a[2]=next--DIRECTORY
    $a[3]=that--DIRECTORY
    $a[4]=big
    $a[5]=fruit
    $a[6]=this
    Last edited by james438; 11-24-2009 at 02:43 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Looking around I found this thread http://www.dynamicdrive.com/forums/s...ght=array+sort and tweaked the code to look like this:

    PHP Code:
    <?php
    function parseMD($date) {
      
    $end   substr($date,-9);
      
    $begin substr($date,0,20);
      return array(
        
    'm' => $end,
        
    'd' => $begin
      
    );
    }
    function 
    compareMD($a$b) {
      
    $da parseMD($b);
      
    $db parseMD($a);
      
    $ret 0;

      foreach(array(
    'd''m') as $k)
        if(
    $da[$k] < $db[$k])
          
    $ret 1;
        else if(
    $da[$k] > $db[$k])
          
    $ret = -1;
      return 
    $ret;
    }
    $ab=array('that--DIRECTORY','next--DIRECTORY','big','this','dad--DIRECTORY','fruit','apple--DIRECTORY');

    usort($ab"compareMD");
    print_r($ab);
    ?>
    However, now the array looks like this:
    Code:
    Array ( [0] => big [1] => fruit [2] => this [3] => apple--DIRECTORY [4] => dad--DIRECTORY [5] => next--DIRECTORY [6] => that--DIRECTORY )
    which is really close, but the "--DIRECTORY" listings are placed second as opposed to first. I really do not understand user defined functions at all.

    I never did learn how to create functions. user defined functions may be easy compared to the PHP library as a whole, but it is like organic chemistry to me.
    Last edited by james438; 11-23-2009 at 10:05 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Well, after a few hours of staring at the functions I last posted, reading up on different functions and trying out different things I was not able to figure much out.

    But I was able to come up with the following, which does what I want:

    PHP Code:
    <?php
    function tag($a$b) {return (substr($a,-9) > substr($b,-9));}
    $a=array('that--DIRECTORY','next--DIRECTORY','big','this','dad--DIRECTORY','fruit','apple--DIRECTORY','aaaaaaaaaaaa');
    usort($a,'tag');
    foreach (
    $a as $k => $v)
    {
    $pos strpos($v'--DIRECTORY');
    if (
    $pos !== false)
    {
    $pos=$k;
    break;
    }
    }

    foreach (
    $a as $k => $v)
    {
    $pos1 strpos($v'--DIRECTORY');
    if (
    $pos1 !== false)
    {
    $pos2++;
    }
    }

    $begin=array_splice($a,$pos,$pos2);
    natcasesort($begin);
    $end=$a;
    natcasesort($end);
    $a=array_merge($begin,$end);
    if (
    $pos===falsenatcasesort($a);
    print_r($a);
    ?>
    produces
    Code:
    Array ( [0] => apple--DIRECTORY [1] => dad--DIRECTORY [2] => next--DIRECTORY [3] => that
    --DIRECTORY [4] => aaaaaaaaaaaa [5] => big [6] => fruit [7] => this )
    I am sure this can be cleaned up and made more efficient, but it works.
    Last edited by james438; 11-24-2009 at 02:43 AM. Reason: rewritten with correct code and post rewritten in general.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    You're thinking too hard. (and you have some syntax problems in your code above.) Try this:
    PHP Code:
    <?php

    // define your list
    $a = array(
        
    'that--DIRECTORY',
        
    'next--DIRECTORY',
        
    'big',
        
    'this',
        
    'dad--DIRECTORY',
        
    'fruit',
        
    'apple--DIRECTORY'
        
    );
    // call the function and pass your list to it
    $sortedList DIRsort($a);

    function 
    DIRsort($list){

        
    // temp array to hold values with "--DIRECTORY"
        
    $dirSubList = array();
        
    // temp array to hold non-"--DIRECTORY" values
        
    $subList = array();

        
    // loop through each item in array
        
    foreach($list as $li){
            
    // if "--DIRECTORY" is found, place value in $dirSubList array
            
    if(strstr($li'--DIRECTORY')){ $dirSubList[] = $li; }
            
    // if not, place in $subList array
            
    else{ $subList[] = $li; }
        }
        
        
    // sort each array alphabetically
        
    sort($dirSubList);
        
    sort($subList);
        
    // recombine the two arrays
        
    $List array_merge($dirSubList$subList);
        
        
    // send the properly sorted list back to the script
        
    return $List;

    }

    ?>
    Tested. var_dump($sortedList); outputs:
    Code:
    array(7) {
      [0]=>
      string(16) "apple--DIRECTORY"
      [1]=>
      string(14) "dad--DIRECTORY"
      [2]=>
      string(15) "next--DIRECTORY"
      [3]=>
      string(15) "that--DIRECTORY"
      [4]=>
      string(3) "big"
      [5]=>
      string(5) "fruit"
      [6]=>
      string(4) "this"
    }
    Last edited by traq; 11-24-2009 at 02:41 AM.

  5. The Following User Says Thank You to traq For This Useful Post:

    james438 (11-24-2009)

  6. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Thank you for that and especially for the extensive notation. I'll be studying it for a bit. User defined functions is my php achilles heel.

    P.S. If you look at the post right before yours you will see the solution that I came up with. I edited the post to remove the errors. Yours is obviously better and easier to understand though.
    To choose the lesser of two evils is still to choose evil. My personal site

  7. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    You're welcome! I think you were on the same track. I'm not sure about some of it though.

    user-defined functions are well worth it. once you get them mastered, learn how to build classes. good luck!

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
  •