Search code examples
perlsortingfilenames

perl - ignore numbers in file name while sorting folder


I would like to sort the files in a folder based only on the non numeric part of the file name. The general pattern of a file name is as follows:

BLA_SomeText_2015-07-16_12-00-05_v2.6.6.6_OtherText.ext
BLA_SomeText_2015-07-16_12-00-05_v2.6.6.7_Other.ext

The expected order of the sorted files is the other way around as above:

BLA_SomeText_2015-07-16_12-00-05_v2.6.6.7_Other.ext
BLA_SomeText_2015-07-16_12-00-05_v2.6.6.6_OtherText.ext

Is there a variant of sort that I missed from the perl documentation or I need to use regex anyhow? Thank you for your help.


Solution

  • It will remove all numbers and then string sort in ascending order,

    my @arr = qw(
      BLA_SomeText_2015-07-16_12-00-05_v2.6.6.6_OtherText.ext
      BLA_SomeText_2015-07-16_12-00-05_v2.6.6.7_Other.ext
    );
    
    @arr = map $_->[0],
      sort { $a->[1] cmp $b->[1] }
      map [ $_, tr|0-9||dr ], @arr;