Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Sort directories by date

by Laurent_R (Canon)
on Sep 29, 2015 at 10:30 UTC ( [id://1143330]=note: print w/replies, xml ) Need Help??


in reply to Sort directories by date

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.

Replies are listed 'Best First'.
Re^2: Sort directories by date
by AnomalousMonk (Archbishop) on Sep 29, 2015 at 16:18 UTC

    It just occurred to me that a GRTish, guaranteed-single-pass solution is possible:

    c:\@Work\Perl\monks>perl -wMstrict -le "use List::Util qw(maxstr); ;; my @dates = qw( 12112014 12012014 01052015 12202014 12022014 02202015 03102015 01012011 09092015 04092015 ); ;; my $most_recent = unpack 'x4a*', maxstr map pack('a4a*', unpack('x4a4', $_), $_), @dates ; ;; print $most_recent; " 09092015
    Use  minstr for least-recent date. See the core module List::Util. No efficiency/performance testing done nor claims made. (Update: Actually, I'd be surprised if there's any advantage unless you're dealing with really large lists; default sort (with no subroutine block) is pretty fast!)

    Update: See also pack, unpack, perlpacktut.


    Give a man a fish:  <%-{-{-{-<

      Yes, it is a fairly nice way of doing it.++

      I had also been thinking about some similar form of GR-like transform (though I had not thought about using the maxstr function of List::Until), but I finally preferred to make my algorithmic point with a simple basic straight-forward and manual search of the maximum date.

      I also agree that the various ways of doing that have little consequence on performance unless the list is really very long.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1143330]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found