in reply to Re: Sort directories by date
in thread Sort directories by date

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Sort directories by date
by Laurent_R (Canon) on Sep 29, 2015 at 18:52 UTC
    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.