glob() - sort by date












46















I'm trying to display an array of files in order of date (last modified).



I have done this buy looping through the array and sorting it into another array, but is there an easier (more efficient) way to do this?










share|improve this question


















  • 1





    Did you find this? (Just did a Google search on your question title): webdeveloper.com/forum/showthread.php?t=188670

    – John
    Sep 24 '08 at 1:58
















46















I'm trying to display an array of files in order of date (last modified).



I have done this buy looping through the array and sorting it into another array, but is there an easier (more efficient) way to do this?










share|improve this question


















  • 1





    Did you find this? (Just did a Google search on your question title): webdeveloper.com/forum/showthread.php?t=188670

    – John
    Sep 24 '08 at 1:58














46












46








46


11






I'm trying to display an array of files in order of date (last modified).



I have done this buy looping through the array and sorting it into another array, but is there an easier (more efficient) way to do this?










share|improve this question














I'm trying to display an array of files in order of date (last modified).



I have done this buy looping through the array and sorting it into another array, but is there an easier (more efficient) way to do this?







php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 24 '08 at 1:51









colecole

5712813




5712813








  • 1





    Did you find this? (Just did a Google search on your question title): webdeveloper.com/forum/showthread.php?t=188670

    – John
    Sep 24 '08 at 1:58














  • 1





    Did you find this? (Just did a Google search on your question title): webdeveloper.com/forum/showthread.php?t=188670

    – John
    Sep 24 '08 at 1:58








1




1





Did you find this? (Just did a Google search on your question title): webdeveloper.com/forum/showthread.php?t=188670

– John
Sep 24 '08 at 1:58





Did you find this? (Just did a Google search on your question title): webdeveloper.com/forum/showthread.php?t=188670

– John
Sep 24 '08 at 1:58












4 Answers
4






active

oldest

votes


















88














For the sake of posterity, in case the forum post linked in the accepted answer is lost or unclear to some, the relevant code needed is:



<?php

$myarray = glob("*.*");
usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));

?>


Tested this on my system and verified it does sort by file mtime as desired. I used a similar approach (written in Python) for determining the last updated files on my website as well.






share|improve this answer



















  • 12





    Worked beautifully. I wanted the reverse order, so I swapped $a with $b in the function definition field. Thanks Jay!

    – AVProgrammer
    Jan 11 '12 at 22:53






  • 4





    I just used this again. For posterity indeed!

    – AVProgrammer
    Mar 28 '12 at 21:21






  • 3





    This code is accessing the filesystem every time a comparison is made (several times for each file). Depending where your filesystem is, that could be very slow. Also, if any of the files is written to during the sort, then the changing file times could lead to bizarre sorting results, depending on the algorithm used by usort. I would recommend the other answer, which avoids all these problems.

    – Matt
    Jun 27 '13 at 18:43






  • 4





    Hmm, now I am starting to doubt my choice of career...

    – AVProgrammer
    Dec 18 '13 at 8:43






  • 4





    If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. php.net/create_function

    – xd6_
    Jan 29 '15 at 14:33





















36














<?php
$items = glob('*', GLOB_NOSORT);
array_multisort(array_map('filemtime', $items), SORT_NUMERIC, SORT_DESC, $items);





share|improve this answer





















  • 1





    It makes no sense to pass the result of array_map as an argument meant to be passed by reference. You will sort it but then what? You no longer have it.

    – Okonomiyaki3000
    Apr 15 '14 at 5:47






  • 1





    The result of array_map is used to sort the $items array, which is also passed by reference.

    – Alf Eaton
    Apr 15 '14 at 20:34






  • 1





    I see. I guess this will work, a similar approach is even documented on the array_multisort page of php.net. I think there are more appropriate functions for this task but I'll take back my downvote. Or I would if I could... sorry.

    – Okonomiyaki3000
    Apr 16 '14 at 5:18








  • 2





    @Okonomiyaki3000: I replaced your downvote, since it was ignorant.

    – AbraCadaver
    Mar 10 '16 at 19:36











  • Tested and doesn't sort correctly in real life despite it looks like a sexy oneliner.

    – Viktor Joras
    Jun 25 '18 at 7:17





















12














This solution is same as accepted answer, updated with anonymous function1:



$myarray = glob("*.*");

usort( $myarray, function( $a, $b ) { return filemtime($a) - filemtime($b); } );




1Anonymous functions have been introduced in PHP in 2010. Original answer is dated 2008.






share|improve this answer





















  • 1





    Since it's 2019 now, this should be the accepted answer. PHP 7 no longer allows for "create_function".. so the best solution, across the board, is to use the anonymous function (or a reference)

    – Matt Kenefick
    Jan 9 at 16:07



















0














I know this thread is old, but this can be done with a better performance. The usort() in the accepted answer will call filemtime() a lot of times. PHP uses quicksort algorithm which has an average performance of 1.39*n*lg(n). The algorithm calls filemtime() twice per comparison, so we will have about 28 calls for 10 directory entries, 556 calls for 100 entries, 8340 calls for 1000 entries etc. The following piece of code works good for me and has a great performance:



exec ( stripos ( PHP_OS, 'WIN' ) === 0 ? 'dir /B /O-D *.*' : 'ls -td1 *.*' , $myarray );





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f124958%2fglob-sort-by-date%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    88














    For the sake of posterity, in case the forum post linked in the accepted answer is lost or unclear to some, the relevant code needed is:



    <?php

    $myarray = glob("*.*");
    usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));

    ?>


    Tested this on my system and verified it does sort by file mtime as desired. I used a similar approach (written in Python) for determining the last updated files on my website as well.






    share|improve this answer



















    • 12





      Worked beautifully. I wanted the reverse order, so I swapped $a with $b in the function definition field. Thanks Jay!

      – AVProgrammer
      Jan 11 '12 at 22:53






    • 4





      I just used this again. For posterity indeed!

      – AVProgrammer
      Mar 28 '12 at 21:21






    • 3





      This code is accessing the filesystem every time a comparison is made (several times for each file). Depending where your filesystem is, that could be very slow. Also, if any of the files is written to during the sort, then the changing file times could lead to bizarre sorting results, depending on the algorithm used by usort. I would recommend the other answer, which avoids all these problems.

      – Matt
      Jun 27 '13 at 18:43






    • 4





      Hmm, now I am starting to doubt my choice of career...

      – AVProgrammer
      Dec 18 '13 at 8:43






    • 4





      If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. php.net/create_function

      – xd6_
      Jan 29 '15 at 14:33


















    88














    For the sake of posterity, in case the forum post linked in the accepted answer is lost or unclear to some, the relevant code needed is:



    <?php

    $myarray = glob("*.*");
    usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));

    ?>


    Tested this on my system and verified it does sort by file mtime as desired. I used a similar approach (written in Python) for determining the last updated files on my website as well.






    share|improve this answer



















    • 12





      Worked beautifully. I wanted the reverse order, so I swapped $a with $b in the function definition field. Thanks Jay!

      – AVProgrammer
      Jan 11 '12 at 22:53






    • 4





      I just used this again. For posterity indeed!

      – AVProgrammer
      Mar 28 '12 at 21:21






    • 3





      This code is accessing the filesystem every time a comparison is made (several times for each file). Depending where your filesystem is, that could be very slow. Also, if any of the files is written to during the sort, then the changing file times could lead to bizarre sorting results, depending on the algorithm used by usort. I would recommend the other answer, which avoids all these problems.

      – Matt
      Jun 27 '13 at 18:43






    • 4





      Hmm, now I am starting to doubt my choice of career...

      – AVProgrammer
      Dec 18 '13 at 8:43






    • 4





      If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. php.net/create_function

      – xd6_
      Jan 29 '15 at 14:33
















    88












    88








    88







    For the sake of posterity, in case the forum post linked in the accepted answer is lost or unclear to some, the relevant code needed is:



    <?php

    $myarray = glob("*.*");
    usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));

    ?>


    Tested this on my system and verified it does sort by file mtime as desired. I used a similar approach (written in Python) for determining the last updated files on my website as well.






    share|improve this answer













    For the sake of posterity, in case the forum post linked in the accepted answer is lost or unclear to some, the relevant code needed is:



    <?php

    $myarray = glob("*.*");
    usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));

    ?>


    Tested this on my system and verified it does sort by file mtime as desired. I used a similar approach (written in Python) for determining the last updated files on my website as well.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 24 '08 at 2:19









    JayJay

    35.1k115580




    35.1k115580








    • 12





      Worked beautifully. I wanted the reverse order, so I swapped $a with $b in the function definition field. Thanks Jay!

      – AVProgrammer
      Jan 11 '12 at 22:53






    • 4





      I just used this again. For posterity indeed!

      – AVProgrammer
      Mar 28 '12 at 21:21






    • 3





      This code is accessing the filesystem every time a comparison is made (several times for each file). Depending where your filesystem is, that could be very slow. Also, if any of the files is written to during the sort, then the changing file times could lead to bizarre sorting results, depending on the algorithm used by usort. I would recommend the other answer, which avoids all these problems.

      – Matt
      Jun 27 '13 at 18:43






    • 4





      Hmm, now I am starting to doubt my choice of career...

      – AVProgrammer
      Dec 18 '13 at 8:43






    • 4





      If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. php.net/create_function

      – xd6_
      Jan 29 '15 at 14:33
















    • 12





      Worked beautifully. I wanted the reverse order, so I swapped $a with $b in the function definition field. Thanks Jay!

      – AVProgrammer
      Jan 11 '12 at 22:53






    • 4





      I just used this again. For posterity indeed!

      – AVProgrammer
      Mar 28 '12 at 21:21






    • 3





      This code is accessing the filesystem every time a comparison is made (several times for each file). Depending where your filesystem is, that could be very slow. Also, if any of the files is written to during the sort, then the changing file times could lead to bizarre sorting results, depending on the algorithm used by usort. I would recommend the other answer, which avoids all these problems.

      – Matt
      Jun 27 '13 at 18:43






    • 4





      Hmm, now I am starting to doubt my choice of career...

      – AVProgrammer
      Dec 18 '13 at 8:43






    • 4





      If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. php.net/create_function

      – xd6_
      Jan 29 '15 at 14:33










    12




    12





    Worked beautifully. I wanted the reverse order, so I swapped $a with $b in the function definition field. Thanks Jay!

    – AVProgrammer
    Jan 11 '12 at 22:53





    Worked beautifully. I wanted the reverse order, so I swapped $a with $b in the function definition field. Thanks Jay!

    – AVProgrammer
    Jan 11 '12 at 22:53




    4




    4





    I just used this again. For posterity indeed!

    – AVProgrammer
    Mar 28 '12 at 21:21





    I just used this again. For posterity indeed!

    – AVProgrammer
    Mar 28 '12 at 21:21




    3




    3





    This code is accessing the filesystem every time a comparison is made (several times for each file). Depending where your filesystem is, that could be very slow. Also, if any of the files is written to during the sort, then the changing file times could lead to bizarre sorting results, depending on the algorithm used by usort. I would recommend the other answer, which avoids all these problems.

    – Matt
    Jun 27 '13 at 18:43





    This code is accessing the filesystem every time a comparison is made (several times for each file). Depending where your filesystem is, that could be very slow. Also, if any of the files is written to during the sort, then the changing file times could lead to bizarre sorting results, depending on the algorithm used by usort. I would recommend the other answer, which avoids all these problems.

    – Matt
    Jun 27 '13 at 18:43




    4




    4





    Hmm, now I am starting to doubt my choice of career...

    – AVProgrammer
    Dec 18 '13 at 8:43





    Hmm, now I am starting to doubt my choice of career...

    – AVProgrammer
    Dec 18 '13 at 8:43




    4




    4





    If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. php.net/create_function

    – xd6_
    Jan 29 '15 at 14:33







    If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. php.net/create_function

    – xd6_
    Jan 29 '15 at 14:33















    36














    <?php
    $items = glob('*', GLOB_NOSORT);
    array_multisort(array_map('filemtime', $items), SORT_NUMERIC, SORT_DESC, $items);





    share|improve this answer





















    • 1





      It makes no sense to pass the result of array_map as an argument meant to be passed by reference. You will sort it but then what? You no longer have it.

      – Okonomiyaki3000
      Apr 15 '14 at 5:47






    • 1





      The result of array_map is used to sort the $items array, which is also passed by reference.

      – Alf Eaton
      Apr 15 '14 at 20:34






    • 1





      I see. I guess this will work, a similar approach is even documented on the array_multisort page of php.net. I think there are more appropriate functions for this task but I'll take back my downvote. Or I would if I could... sorry.

      – Okonomiyaki3000
      Apr 16 '14 at 5:18








    • 2





      @Okonomiyaki3000: I replaced your downvote, since it was ignorant.

      – AbraCadaver
      Mar 10 '16 at 19:36











    • Tested and doesn't sort correctly in real life despite it looks like a sexy oneliner.

      – Viktor Joras
      Jun 25 '18 at 7:17


















    36














    <?php
    $items = glob('*', GLOB_NOSORT);
    array_multisort(array_map('filemtime', $items), SORT_NUMERIC, SORT_DESC, $items);





    share|improve this answer





















    • 1





      It makes no sense to pass the result of array_map as an argument meant to be passed by reference. You will sort it but then what? You no longer have it.

      – Okonomiyaki3000
      Apr 15 '14 at 5:47






    • 1





      The result of array_map is used to sort the $items array, which is also passed by reference.

      – Alf Eaton
      Apr 15 '14 at 20:34






    • 1





      I see. I guess this will work, a similar approach is even documented on the array_multisort page of php.net. I think there are more appropriate functions for this task but I'll take back my downvote. Or I would if I could... sorry.

      – Okonomiyaki3000
      Apr 16 '14 at 5:18








    • 2





      @Okonomiyaki3000: I replaced your downvote, since it was ignorant.

      – AbraCadaver
      Mar 10 '16 at 19:36











    • Tested and doesn't sort correctly in real life despite it looks like a sexy oneliner.

      – Viktor Joras
      Jun 25 '18 at 7:17
















    36












    36








    36







    <?php
    $items = glob('*', GLOB_NOSORT);
    array_multisort(array_map('filemtime', $items), SORT_NUMERIC, SORT_DESC, $items);





    share|improve this answer















    <?php
    $items = glob('*', GLOB_NOSORT);
    array_multisort(array_map('filemtime', $items), SORT_NUMERIC, SORT_DESC, $items);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 21 '10 at 11:46

























    answered Jul 21 '10 at 11:40









    Alf EatonAlf Eaton

    2,95232936




    2,95232936








    • 1





      It makes no sense to pass the result of array_map as an argument meant to be passed by reference. You will sort it but then what? You no longer have it.

      – Okonomiyaki3000
      Apr 15 '14 at 5:47






    • 1





      The result of array_map is used to sort the $items array, which is also passed by reference.

      – Alf Eaton
      Apr 15 '14 at 20:34






    • 1





      I see. I guess this will work, a similar approach is even documented on the array_multisort page of php.net. I think there are more appropriate functions for this task but I'll take back my downvote. Or I would if I could... sorry.

      – Okonomiyaki3000
      Apr 16 '14 at 5:18








    • 2





      @Okonomiyaki3000: I replaced your downvote, since it was ignorant.

      – AbraCadaver
      Mar 10 '16 at 19:36











    • Tested and doesn't sort correctly in real life despite it looks like a sexy oneliner.

      – Viktor Joras
      Jun 25 '18 at 7:17
















    • 1





      It makes no sense to pass the result of array_map as an argument meant to be passed by reference. You will sort it but then what? You no longer have it.

      – Okonomiyaki3000
      Apr 15 '14 at 5:47






    • 1





      The result of array_map is used to sort the $items array, which is also passed by reference.

      – Alf Eaton
      Apr 15 '14 at 20:34






    • 1





      I see. I guess this will work, a similar approach is even documented on the array_multisort page of php.net. I think there are more appropriate functions for this task but I'll take back my downvote. Or I would if I could... sorry.

      – Okonomiyaki3000
      Apr 16 '14 at 5:18








    • 2





      @Okonomiyaki3000: I replaced your downvote, since it was ignorant.

      – AbraCadaver
      Mar 10 '16 at 19:36











    • Tested and doesn't sort correctly in real life despite it looks like a sexy oneliner.

      – Viktor Joras
      Jun 25 '18 at 7:17










    1




    1





    It makes no sense to pass the result of array_map as an argument meant to be passed by reference. You will sort it but then what? You no longer have it.

    – Okonomiyaki3000
    Apr 15 '14 at 5:47





    It makes no sense to pass the result of array_map as an argument meant to be passed by reference. You will sort it but then what? You no longer have it.

    – Okonomiyaki3000
    Apr 15 '14 at 5:47




    1




    1





    The result of array_map is used to sort the $items array, which is also passed by reference.

    – Alf Eaton
    Apr 15 '14 at 20:34





    The result of array_map is used to sort the $items array, which is also passed by reference.

    – Alf Eaton
    Apr 15 '14 at 20:34




    1




    1





    I see. I guess this will work, a similar approach is even documented on the array_multisort page of php.net. I think there are more appropriate functions for this task but I'll take back my downvote. Or I would if I could... sorry.

    – Okonomiyaki3000
    Apr 16 '14 at 5:18







    I see. I guess this will work, a similar approach is even documented on the array_multisort page of php.net. I think there are more appropriate functions for this task but I'll take back my downvote. Or I would if I could... sorry.

    – Okonomiyaki3000
    Apr 16 '14 at 5:18






    2




    2





    @Okonomiyaki3000: I replaced your downvote, since it was ignorant.

    – AbraCadaver
    Mar 10 '16 at 19:36





    @Okonomiyaki3000: I replaced your downvote, since it was ignorant.

    – AbraCadaver
    Mar 10 '16 at 19:36













    Tested and doesn't sort correctly in real life despite it looks like a sexy oneliner.

    – Viktor Joras
    Jun 25 '18 at 7:17







    Tested and doesn't sort correctly in real life despite it looks like a sexy oneliner.

    – Viktor Joras
    Jun 25 '18 at 7:17













    12














    This solution is same as accepted answer, updated with anonymous function1:



    $myarray = glob("*.*");

    usort( $myarray, function( $a, $b ) { return filemtime($a) - filemtime($b); } );




    1Anonymous functions have been introduced in PHP in 2010. Original answer is dated 2008.






    share|improve this answer





















    • 1





      Since it's 2019 now, this should be the accepted answer. PHP 7 no longer allows for "create_function".. so the best solution, across the board, is to use the anonymous function (or a reference)

      – Matt Kenefick
      Jan 9 at 16:07
















    12














    This solution is same as accepted answer, updated with anonymous function1:



    $myarray = glob("*.*");

    usort( $myarray, function( $a, $b ) { return filemtime($a) - filemtime($b); } );




    1Anonymous functions have been introduced in PHP in 2010. Original answer is dated 2008.






    share|improve this answer





















    • 1





      Since it's 2019 now, this should be the accepted answer. PHP 7 no longer allows for "create_function".. so the best solution, across the board, is to use the anonymous function (or a reference)

      – Matt Kenefick
      Jan 9 at 16:07














    12












    12








    12







    This solution is same as accepted answer, updated with anonymous function1:



    $myarray = glob("*.*");

    usort( $myarray, function( $a, $b ) { return filemtime($a) - filemtime($b); } );




    1Anonymous functions have been introduced in PHP in 2010. Original answer is dated 2008.






    share|improve this answer















    This solution is same as accepted answer, updated with anonymous function1:



    $myarray = glob("*.*");

    usort( $myarray, function( $a, $b ) { return filemtime($a) - filemtime($b); } );




    1Anonymous functions have been introduced in PHP in 2010. Original answer is dated 2008.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 23 '17 at 11:47









    Community

    11




    11










    answered Mar 10 '16 at 19:33









    fusion3kfusion3k

    9,93941535




    9,93941535








    • 1





      Since it's 2019 now, this should be the accepted answer. PHP 7 no longer allows for "create_function".. so the best solution, across the board, is to use the anonymous function (or a reference)

      – Matt Kenefick
      Jan 9 at 16:07














    • 1





      Since it's 2019 now, this should be the accepted answer. PHP 7 no longer allows for "create_function".. so the best solution, across the board, is to use the anonymous function (or a reference)

      – Matt Kenefick
      Jan 9 at 16:07








    1




    1





    Since it's 2019 now, this should be the accepted answer. PHP 7 no longer allows for "create_function".. so the best solution, across the board, is to use the anonymous function (or a reference)

    – Matt Kenefick
    Jan 9 at 16:07





    Since it's 2019 now, this should be the accepted answer. PHP 7 no longer allows for "create_function".. so the best solution, across the board, is to use the anonymous function (or a reference)

    – Matt Kenefick
    Jan 9 at 16:07











    0














    I know this thread is old, but this can be done with a better performance. The usort() in the accepted answer will call filemtime() a lot of times. PHP uses quicksort algorithm which has an average performance of 1.39*n*lg(n). The algorithm calls filemtime() twice per comparison, so we will have about 28 calls for 10 directory entries, 556 calls for 100 entries, 8340 calls for 1000 entries etc. The following piece of code works good for me and has a great performance:



    exec ( stripos ( PHP_OS, 'WIN' ) === 0 ? 'dir /B /O-D *.*' : 'ls -td1 *.*' , $myarray );





    share|improve this answer




























      0














      I know this thread is old, but this can be done with a better performance. The usort() in the accepted answer will call filemtime() a lot of times. PHP uses quicksort algorithm which has an average performance of 1.39*n*lg(n). The algorithm calls filemtime() twice per comparison, so we will have about 28 calls for 10 directory entries, 556 calls for 100 entries, 8340 calls for 1000 entries etc. The following piece of code works good for me and has a great performance:



      exec ( stripos ( PHP_OS, 'WIN' ) === 0 ? 'dir /B /O-D *.*' : 'ls -td1 *.*' , $myarray );





      share|improve this answer


























        0












        0








        0







        I know this thread is old, but this can be done with a better performance. The usort() in the accepted answer will call filemtime() a lot of times. PHP uses quicksort algorithm which has an average performance of 1.39*n*lg(n). The algorithm calls filemtime() twice per comparison, so we will have about 28 calls for 10 directory entries, 556 calls for 100 entries, 8340 calls for 1000 entries etc. The following piece of code works good for me and has a great performance:



        exec ( stripos ( PHP_OS, 'WIN' ) === 0 ? 'dir /B /O-D *.*' : 'ls -td1 *.*' , $myarray );





        share|improve this answer













        I know this thread is old, but this can be done with a better performance. The usort() in the accepted answer will call filemtime() a lot of times. PHP uses quicksort algorithm which has an average performance of 1.39*n*lg(n). The algorithm calls filemtime() twice per comparison, so we will have about 28 calls for 10 directory entries, 556 calls for 100 entries, 8340 calls for 1000 entries etc. The following piece of code works good for me and has a great performance:



        exec ( stripos ( PHP_OS, 'WIN' ) === 0 ? 'dir /B /O-D *.*' : 'ls -td1 *.*' , $myarray );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 30 at 16:19









        SebastianSebastian

        124




        124






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f124958%2fglob-sort-by-date%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules