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 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How to sort array of directory names into order based on file value

    Hi all - I need to sort an array and I am really struggling to get my head around this and wondered if anyone could help please ?

    Here is the situation :

    There are a bunch of directories (number unknown).

    In each directory are two files (file1 and file2) - Each file contains a simple integer.

    I need to collect the names of all the directories into an array, walk through the array and pull the integers from the two files in each directory and multiply the integers together.

    I then need to sort the array of directory names in descending order by the value gained by multiplying the two numbers.

    I can obvously get the dirnames into an array as follows :

    $dirs = glob('*',GLOB_NOSORT|GLOB_ONLYDIR);

    I can then walk through this array and pull the numbers :

    foreach ($dirs as $directory) {
    $number1=file_get_contents($directory."/file1");
    $number2=file_get_contents($directory."/file2");
    $result=$number1&number2;
    }

    Here is where I am stuck LOL! I thought that maybe I can convert the one-dimensional $dirs array into a two-dimensional and add the $result variable to the corresponding entry in $dirs and then, maybe, at the end do some sort of usort on the $dirs array.

    I just don't know where to go from here.

    What I want to end up with is an array of directory names that is sorted by the product of two values stored in two files within each directory (if that makes sense).

    Any help would really be super appreciated.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    To do this, you need to write a custom comparator. Fortunately it's super easy to do. You can fetch the directory names however you want.
    This is the absolute most basic example I can think of:
    PHP Code:

    function cmpDirectoryByFileMultiply($sDir1$sDir2)
    {
        static 
    $aDirMulti = array();
        
    $iResult 0;
        
    $d1f1 $sDir1 '/file1';
        
    $d1f2 $sDir1 '/file2';
        
    $d2f1 $sDir2 '/file1';
        
    $d2f2 $sDir2 '/file2';

        if (
    file_exists($d1f1) && file_exists($d1f2) && file_exists($d2f1) && file_exists($d2f2))
        {
            if (!isset(
    $aDirMulti[$sDir1]))
            {
                
    $aDirMulti[$sDir1] = file_get_contents($d1f1) * file_get_contents($d1f2);
            }
            if (!isset(
    $aDirMulti[$sDir2]))
            {
                
    $aDirMulti[$sDir2] = file_get_contents($d2f1) * file_get_contents($d2f2);
            }

            
    $iResult $aDirMulti[$sDir1] - $aDirMulti[$sDir2];
        }
        return 
    $iResult;
    }

    usort($dirs'cmpDirectoryByFileMultiply'); 
    Untested and no error handling, but otherwise works okay in my head.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    spman (05-13-2013)

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Fou-Lu - thank you so much By the time I read the post I had already come up with something similar but your version is much more elegant


  •  

    Tags for this Thread

    Posting Permissions

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