in reply to Sorting filenames with custom function

The real "trick" is to get the regex right that extracts the values from $a and $b. One way is:

#!/usr/bin/perl -w use strict; my @names = qw( FLY_ABC123_01.jpg PG_ABC123_03.jpg SPL_ABC123_02.jpg ); @names = sort{ (my $A) = ($a =~ /.*_(\w+)/); (my $B) = ($b =~ /.*_(\w+)/); $A <=> $B }@names; print "@names"; #prints: FLY_ABC123_01.jpg SPL_ABC123_02.jpg PG_ABC123_03.jpg
Update:(\d+) is fine here instead of (\w+) as per suggestion from AnomalousMonk