in reply to Sorting files by 3 numbers in the name

Since perl's sort is now stable, I offer this in loving memory and tribute to IBM card sorters :)

#!/usr/bin/perl # http://perlmonks.org/?node_id=1191282 use strict; use warnings; use Data::Dumper; 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 ); # sort by pseudo-column with stable sort -- IBM card sorters forever ! +!! my @returnfiles = sort { (split /_/, $a)[1] <=> (split /_/, $b)[1] } sort { (split /_/, $a)[7] <=> (split /_/, $b)[7] } sort { (split /_/, $a)[8] <=> (split /_/, $b)[8] } sort { (split /_/, $a)[9] <=> (split /_/, $b)[9] } @files; print Dumper \@returnfiles;

Replies are listed 'Best First'.
Re^2: Sorting files by 3 numbers in the name
by tobyink (Canon) on May 26, 2017 at 14:54 UTC

    If you've got hundreds of filenames, I think you'll find my way runs significantly faster — it only needs to match each filename against the regexp once. Yours will do it dozens of times per filename.

      Hey, don't besmirch the memory of IBM card sorters.

      Without them, where would 1950's science fiction movies be?

Re^2: Sorting files by 3 numbers in the name
by LanX (Saint) on May 26, 2017 at 15:36 UTC