in reply to Sort directories by date
Or the short-answer, for those who don’t want to answer a quiz to get it, would be ... to use a sort-compare subroutine, inline or otherwise, along these lines: (untested)
sort { substr($a, 4, 4) cmp substr($b, 4, 4) || substr($a, 2, 2) cmp substr($b, 2, 2) || substr($a, 0, 2) cmp substr($b, 0, 2) } ...
The sort verb accepts as its first argument a function that, given two “magic variables” $a and $b, must return a value that is less than, equal to, or greater than zero. The <=> (numeric) and cmp (string) operators are specifically designed for this purpose. Here, in a simple in-line subroutine, we use the || logic-OR operator, which we know uses “short circuiting,” to return the first of three alternatives that is not zero. First, we compare the year. Then, the month, then the day. (The first position in a Perl string is position zero.)
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sort directories by date
by johngg (Canon) on Sep 28, 2015 at 21:33 UTC |