Question 1: why is it sorting the future date at the top of the list?
Because you are sorting lexically and F comes before T and W.
use strict;
use warnings;
use Time::Piece;
use Time::Seconds;
my $dateformat = "%H:%M:%S, %m/%d/%Y";
my $date1 = "11:56:41, 09/14/2022";
my $date2 = "11:22:41, 09/16/2022";
my $date3 = "11:20:41, 09/13/2022";
$date1 = Time::Piece->strptime($date1, $dateformat);
$date2 = Time::Piece->strptime($date2, $dateformat);
$date3 = Time::Piece->strptime($date3, $dateformat);
my(@ds) = ($date1,$date2,$date3);
my(@sorted) = sort(@ds);
print "Lexically\n";
foreach my $dt (@sorted){
print $dt, $/;
}
@sorted = sort { $a->epoch <=> $b->epoch } @ds;
print "\nTemporally\n";
foreach my $dt (@sorted){
print $dt, $/;
}
Question 2: is using Time::Piece->(strptime) the best way to create objects for each of my variables? I didn't see any other way to do that in the doc.
Yes, it probably is the best way. Note that you could use arrays throughout instead of $date1, $date2, $date3, etc.