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.


🦛


In reply to Re: Time::Piece puzzlement by hippo
in thread Time::Piece puzzlement by slugger415

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.