#!/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