I this what you needed? I'm not sure?

Update: I saw the post from johngg at Re^7: Sort files by date in the file name.. I had completely missed the fact that the date format is different in this second data set. If this is the format, then a simple numeric date comparison won't work and something more involved is required. However, when I see at date like 28062017102508, in 2017, that makes me think that this data was hand generated. Note to OP when you post example data, make it as close to the "real thing" as possible. These details matter.

Also, the sort method I show below is basic. Although straight forward, it has a built in inefficiency in that the the splits are run each time a pair of data in $a,$b is selected by the sort algorithm. The Schwartzian Transform as shown by kcott avoids this by calculating the split for all the data at one time. So it is faster, but at the cost of more complexity. I recommend to a beginner to master the basics first, then get fancy once you have a solid foundation.

#!/usr/bin/perl use strict; use warnings; my @files; while (<DATA>) { chomp; push @files, $_; } @files = sort by_first_third @files; sub by_first_third { my ($Afirst,$Athird) = (split '_|\.',$a)[0,2]; my ($Bfirst,$Bthird) = (split '_|\.',$b)[0,2]; $Afirst <=> $Bfirst or $Athird <=> $Bthird } print join ("\n",@files), "\n\n"; print "first file: $files[0]", "\n"; print "last file : $files[-1]", "\n"; =prints 971305332_XXXXXX12345678765463565E_28062011102508.TXT 971305332_AAAAAAAA12345678765463565E_28062011102508.TXT 971305332_CCCC12345678765463565E_28062011102508.TXT 971305332_CCCC12345678765463565E_28062020102508.TXT 981139804_ABCDEF12345678765463565E_28062016102508.TXT 981139804_ABCDEF12345678765463565E_28062016112508.TXT 981139804_ABCDEF12345678765463565E_28062016172508.TXT 981139804_ABCDEF12345678765463565E_28062017102508.TXT first file: 971305332_XXXXXX12345678765463565E_28062011102508.TXT last file : 981139804_ABCDEF12345678765463565E_28062017102508.TXT =cut __DATA__ 981139804_ABCDEF12345678765463565E_28062016172508.TXT 981139804_ABCDEF12345678765463565E_28062016112508.TXT 981139804_ABCDEF12345678765463565E_28062016102508.TXT 981139804_ABCDEF12345678765463565E_28062017102508.TXT 971305332_XXXXXX12345678765463565E_28062011102508.TXT 971305332_AAAAAAAA12345678765463565E_28062011102508.TXT 971305332_CCCC12345678765463565E_28062011102508.TXT 971305332_CCCC12345678765463565E_28062020102508.TXT

In reply to Re: Sort files by date in the file name. by Marshall
in thread Sort files by date in the file name. by leoberbert

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.