There are many possible ways to achieve desired result.
Note: code assumed based that you do not try to sort on club, date_hour_min and day_time at once as it not declared in your question. A sample of desired sorted output would be helpful for clearance of your problem -- we can not read what is on your mind.
Please research following two possible approaches.
You declared sort order based on time day index in the strings -- lets put it to use in some way. I will use AM,AM2,AFT,PM,EVE,NIGHT
as initialization string for simplicity.
First approach utilizes %order
hash with time day be a key and digit representing numerical order. Strings stored in HoA with numerical order as a key. Once you filled the hash just print according numerical key order with lines preserving order of their appearance on input.
use strict;
use warnings;
use feature 'say';
my $count = 0;
my @set = split ',', 'AM,AM2,AFT,PM,EVE,NIGHT';
my %order = map { $_ => $count++ } @set;
my %result;
while( <DATA> ) {
chomp;
for my $k ( keys %order ) {
push @{$result{$order{$k}}}, $_ if /_$k\z/;
}
}
for( sort {$a <=> $b} keys %result ) {
say for @{ $result{$_} };
}
__DATA__
CLUB1_20201008_EVE
CLUB1_20201008_AFT
CLUB1_20201008_AM
CLUB1_20201008_AM2
CLUB1_20201008_PM
CLUB1_20201008_NIGHT
CLUB2_20201008_EVE
CLUB2_20201008_AFT
CLUB2_20201008_AM
CLUB1_20201008_AM2
CLUB1_20201008_PM
CLUB1_20201008_NIGHT
Output
CLUB1_20201008_AM
CLUB2_20201008_AM
CLUB1_20201008_AM2
CLUB1_20201008_AM2
CLUB1_20201008_AFT
CLUB2_20201008_AFT
CLUB1_20201008_PM
CLUB1_20201008_PM
CLUB1_20201008_EVE
CLUB2_20201008_EVE
CLUB1_20201008_NIGHT
CLUB1_20201008_NIGHT
Second approach is even more simple. Push the lines in HoA %result
based on time day index (at the end of the line). Then print print HoA according predefined $order
array.
use strict;
use warnings;
use feature 'say';
my @order = split ',', 'AM,AM2,AFT,PM,EVE,NIGHT';
my %result;
while( <DATA> ) {
chomp;
push @{$result{$1}}, $_ if /_([^_]+)\z/;
}
for( @order ) {
say for @{ $result{$_} };
}
__DATA__
CLUB1_20201008_EVE
CLUB1_20201008_AFT
CLUB1_20201008_AM
CLUB1_20201008_AM2
CLUB1_20201008_PM
CLUB1_20201008_NIGHT
CLUB2_20201008_EVE
CLUB2_20201008_AFT
CLUB2_20201008_AM
CLUB1_20201008_AM2
CLUB1_20201008_PM
CLUB1_20201008_NIGHT
Output
CLUB1_20201008_AM
CLUB2_20201008_AM
CLUB1_20201008_AM2
CLUB1_20201008_AM2
CLUB1_20201008_AFT
CLUB2_20201008_AFT
CLUB1_20201008_PM
CLUB1_20201008_PM
CLUB1_20201008_EVE
CLUB2_20201008_EVE
CLUB1_20201008_NIGHT
CLUB1_20201008_NIGHT