array_multisort()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schraalhans Keukenmeester

    array_multisort()

    I'm having a hard time getting to grips with this function.

    I have an array shaped like this:

    $dircontents[0]['filename'] ='index.html'
    $dircontents[0]['owner'] = 'www'
    $dircontents[0]['group'] = 'www'
    $dircontents[0]['type'] = 'file'

    $dircontents[1]['filename'] ='images'
    $dircontents[1]['owner'] = 'www'
    $dircontents[1]['group'] = 'www'
    $dircontents[1]['type'] = 'dir'

    etc.

    'type' can be any of {dir,file,link}

    How do I get the array sorted, alphabetically, first by type, then filename?
    My attempts sofar have lead to a seemingly unsorted array, not even
    close to anything recognizable. Is array_multisort () even what I should
    be looking for?

    Help appreciated!
    Sh.
  • ZeldorBlat

    #2
    Re: array_multisort ()

    On Mar 27, 5:20 pm, Schraalhans Keukenmeester <bitbuc...@inva lid.spam>
    wrote:
    I'm having a hard time getting to grips with this function.
    >
    I have an array shaped like this:
    >
    $dircontents[0]['filename'] ='index.html'
    $dircontents[0]['owner'] = 'www'
    $dircontents[0]['group'] = 'www'
    $dircontents[0]['type'] = 'file'
    >
    $dircontents[1]['filename'] ='images'
    $dircontents[1]['owner'] = 'www'
    $dircontents[1]['group'] = 'www'
    $dircontents[1]['type'] = 'dir'
    >
    etc.
    >
    'type' can be any of {dir,file,link}
    >
    How do I get the array sorted, alphabetically, first by type, then filename?
    My attempts sofar have lead to a seemingly unsorted array, not even
    close to anything recognizable. Is array_multisort () even what I should
    be looking for?
    >
    Help appreciated!
    Sh.
    Try usort() instead:

    function cmp($a, $b) {
    if($a['type'] < $b['type'])
    return -1;
    elseif($a['type'] $b['type'])
    return 1;
    elseif($a['filename'] < $b['filename'])
    return -1;
    elseif($a['filename'] $b['filename'])
    return 1;
    else
    return 0;
    }

    usort($dirconte nts, 'cmp');

    Comment

    • Schraalhans Keukenmeester

      #3
      Re: array_multisort ()

      ZeldorBlat wrote:
      On Mar 27, 5:20 pm, Schraalhans Keukenmeester <bitbuc...@inva lid.spam>
      wrote:
      >I'm having a hard time getting to grips with this function.
      >>
      >I have an array shaped like this:
      >>
      >$dircontents[0]['filename'] ='index.html'
      >$dircontents[0]['owner'] = 'www'
      >$dircontents[0]['group'] = 'www'
      >$dircontents[0]['type'] = 'file'
      >>
      >$dircontents[1]['filename'] ='images'
      >$dircontents[1]['owner'] = 'www'
      >$dircontents[1]['group'] = 'www'
      >$dircontents[1]['type'] = 'dir'
      >>
      >etc.
      >>
      >'type' can be any of {dir,file,link}
      >>
      >How do I get the array sorted, alphabetically, first by type, then filename?
      >My attempts sofar have lead to a seemingly unsorted array, not even
      >close to anything recognizable. Is array_multisort () even what I should
      >be looking for?
      >>
      >Help appreciated!
      >Sh.
      >
      Try usort() instead:
      >
      function cmp($a, $b) {
      if($a['type'] < $b['type'])
      return -1;
      elseif($a['type'] $b['type'])
      return 1;
      elseif($a['filename'] < $b['filename'])
      return -1;
      elseif($a['filename'] $b['filename'])
      return 1;
      else
      return 0;
      }
      >
      usort($dirconte nts, 'cmp');
      >
      Great! That's what I needed indeed. Somewhere in the back of my head...
      Thanx Zeldor!

      Comment

      • Schraalhans Keukenmeester

        #4
        Re: array_multisort ()

        Schraalhans Keukenmeester wrote:
        ZeldorBlat wrote:
        >On Mar 27, 5:20 pm, Schraalhans Keukenmeester <bitbuc...@inva lid.spam>
        >wrote:
        >>I'm having a hard time getting to grips with this function.
        >>>
        >>I have an array shaped like this:
        >>>
        >>$dircontent s[0]['filename'] ='index.html'
        >>$dircontent s[0]['owner'] = 'www'
        >>$dircontent s[0]['group'] = 'www'
        >>$dircontent s[0]['type'] = 'file'
        >>>
        >>$dircontent s[1]['filename'] ='images'
        >>$dircontent s[1]['owner'] = 'www'
        >>$dircontent s[1]['group'] = 'www'
        >>$dircontent s[1]['type'] = 'dir'
        >>>
        >>etc.
        >>>
        >>'type' can be any of {dir,file,link}
        >>>
        >>How do I get the array sorted, alphabetically, first by type, then filename?
        >>My attempts sofar have lead to a seemingly unsorted array, not even
        >>close to anything recognizable. Is array_multisort () even what I should
        >>be looking for?
        >>>
        >>Help appreciated!
        >>Sh.
        >Try usort() instead:
        >>
        >function cmp($a, $b) {
        > if($a['type'] < $b['type'])
        > return -1;
        > elseif($a['type'] $b['type'])
        > return 1;
        > elseif($a['filename'] < $b['filename'])
        > return -1;
        > elseif($a['filename'] $b['filename'])
        > return 1;
        > else
        > return 0;
        >}
        >>
        >usort($dircont ents, 'cmp');
        >>
        Great! That's what I needed indeed. Somewhere in the back of my head...
        Thanx Zeldor!
        Had to rewrite it a little since I usesd strings. Final version, in case
        someone else likes to use it as well:

        function cmp($a, $b) {
        $retval = strcmp($a['type'],$b['type']);
        if ($retval === 0)
        $retval = strcmp($a['filename'],$b['filename']);
        return $retval;
        }
        usort($this->dircontents, 'cmp');

        Again, thanks for the pointer!

        Comment

        Working...