Although it may not matter that much (depending on how many subdirectories you have in your root directory), sorting the whole list just to get the most recent item is somewhat inefficient, even with a fast sorting algorithm and using Schwartzian transform or Guttman-Rosler transform, because it requires the computer to do much more work than what is actually needed.

I usually would not care that much about that with a short list of subdirectories, but it sometimes matter that there are more efficient algorithms to pick up the latest (or largest, or smallest, whatever) element in a list.

For example, something like this at the command line (quick test):

$ perl -e ' > my @list = qw/12112014 > 01052015 > 02202015 > 03102015 > 01012011 > 10102014 > 04092015 > 09092015 > 09092013/; > chomp @list; > my $max_y = "0000"; > my $max_d = "0000"; > for my $dir (@list) { > my ($d, $y) = $dir =~ /(\d{4})(\d{4})/; > if ($y > $max_y) { > $max_y = $y; > } elsif ($y == $max_y) { > $max_d = $d if $d > $max_d; > } > } > print "$max_d$max_y\n"; > ' 09092015
This may look slightly more complex, but it is more efficient for a long list of directories. Which is why I would care only if the list is long.

In reply to Re: Sort directories by date by Laurent_R
in thread Sort directories by date by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.