1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP sort()

Discussion in 'PHP' started by darkblade, Mar 2, 2007.

  1. #1
    It seem that we I sort an array with files with different extensions, it groups the extensions together then sorts them.

    Example of a sort array
    a.gif
    z.gif
    b.png
    j.png


    Does anyone know how to do that?
     
    darkblade, Mar 2, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    There might be a better way, but this should work.

    
    function extension_sort(&$files)
    {
    	$allfiles = array();
    
    	foreach ($files AS $file)
    	{
    		$extension = substr(strchr($file, '.'), 1);
    		
    		$allfiles['name'][] = basename($file, ".{$extension}");
    		$allfiles['extension'][] = $extension;
    	}
    	
    	array_multisort($allfiles['extension'], $allfiles['name']);
    	$files = array();
    	
    	foreach ($allfiles['name'] AS $key => $name)
    	{
    		$files[] = "{$name}.{$allfiles[extension][$key]}";
    	}	
    }
    
    PHP:
    Usage example
    
    $files = array('b.png', 'z.gif', 'j.png', 'a.gif');
    
    extension_sort($files);
    
    print_r($files);
    
    /*
    Array
    (
        [0] => a.gif
        [1] => z.gif
        [2] => b.png
        [3] => j.png
    )
    */
    
    
    
    PHP:
     
    nico_swd, Mar 2, 2007 IP
  3. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wouldn't it be better to have it in a two-dimensional array and sort it by extension that way?
     
    bobby9101, Mar 2, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    I think he wants it to sort by extension AND file name.

    If you have a better solution, feel free to share. :)
     
    nico_swd, Mar 2, 2007 IP
  5. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    k...

    <?php
    
    $files = array('a.gif', 'j.png', 'z.gif', 'b.png');
    
    usort($files, 'extSort');
    
    // Sort files by extension, then name
    function extSort($first, $second) {
    
       // Match a filename, dot, and extension
       $pattern = '/^(.*)\.(.*?)?$/';
    
       if (preg_match($pattern, $first, $firstSet) && preg_match($pattern, $second, $secondSet)) {
          list(, $firstName, $firstExt) = $firstSet;
          list(, $secondName, $secondExt) = $secondSet;
    
          // If the extensions are equal, sort by name
          if ($firstExt == $secondExt) {
             return strcmp($firstName, $secondName);
          }
          // Otherwise, sort by extension
          return strcmp($firstExt, $secondExt);
       }
       return 0;
    }
    
    ?>
    PHP:
     
    Robert Plank, Mar 9, 2007 IP