in reply to Sorting files by 3 numbers in the name

I have no idea how fast this module is, but you cannot beat it for convenience.
use strict; use warnings; use List::UtilsBy qw(sort_by); my $x; my @files = qw( ASR0005336_8950_ETSTexas_EOC052017P_0517_Candidate_RRD_178904_01_0 +2_Spr17_Initial_201705040952_41044.zip ASR0004520_8960_ETSTexas_EOC052017P_0517_Candidate_RRD_178901_04_0 +4_Spr17_Initial_201705040952_41045.zip ASR0004994_8958_ETSTexas_EOC052017P_0517_Candidate_RRD_178901_02_0 +4_Spr17_Initial_201705040951_41043.zip ASR0005336_8950_ETSTexas_EOC052017P_0517_Candidate_RRD_178904_02_0 +2_Spr17_Initial_201705040952_41044.zip ASR0005154_8957_ETSTexas_EOC052017P_0517_Candidate_RRD_178901_01_0 +4_Spr17_Initial_201705040951_41042.zip ASR0005336_8959_ETSTexas_EOC052017P_0517_Candidate_RRD_178901_03_0 +4_Spr17_Initial_201705040952_41044.zip ASR0005336_8972_ETSTexas_EOC052017P_0517_Candidate_RRD_178902_01_0 +1_Spr17_Initial_201705040952_41044.zip ); my @sorted_files = sort_by { join( '', (split /_/, $_)[1,7,8,9]) } @fi +les; $, = "\n"; print @sorted_files;
Bill

Replies are listed 'Best First'.
Re^2: Sorting files by 3 numbers in the name
by LanX (Saint) on May 26, 2017 at 20:00 UTC
    Nice, but you should better choose nsort_by()

    From the docs of List::UtilsBy

    > Similar to sort_by but compares its key values numerically.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      We are comparing fixed length strings of digits. The result is the same whether we compare them lexically or numerically. I chose the lexical sort because the strings do not seem to have any numerical significance.

      Thanks for supplying the link to the module documentation.

      UPDATE: Oops! My comment about no numerical significance is wrong. My comment on the subject in level 6 below applies here as well (as long as all fields are of fixed length). I still prefer the lexical sort, but it is harder to justify.

      Bill

        Untested, but this should pad your numbers with leading zeros, just in case the numbers don't turn out to be fixed length.

        my @sorted_files = sort_by { sprintf( '%012d' x 4, (split /_/, $_)[1,7 +,8,9]) } @files;
        > We are comparing fixed length strings of digits

        I haven't seen that guaranteed, so I prefer playing safe.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!