sub file_ext { # find the last part of each string $a =~ s/.*(\..*)/; $a1 = $1; $b =~ s/.*(\..*)/; $b1 = $1; #split '.', 1, $a; #split '.', 1, $b; #compare them #return $a cmp $b; $a1 cmp $1; }