Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Sort by Date and Time

by ikegami (Patriarch)
on Jan 31, 2009 at 03:13 UTC ( [id://740373]=note: print w/replies, xml ) Need Help??


in reply to Sort by Date and Time

How about
#!/usr/bin/perl use strict; use warnings; my @dates = ( '01-30 22:10', '01-12 05:56', '01-24 01:42', '01-12 05:59', '01-31 01:33', '01-02 01:33' ); @dates = sort @dates; for (@dates) { print $_, "\n"; }

Replies are listed 'Best First'.
Re^2: Sort by Date and Time
by zod (Scribe) on Jan 31, 2009 at 03:22 UTC
    Hmm. I assumed I had to convert dates to datetimes. I knew it looked wrong, though. thanks
      Generally, yes. In this case, they're already in a sortable format. The fields are fixed width and they are ordered by decreasing significance.

      Your code carefully trimmed leading and trailing spaces. To be sure of the sort working on the strings you might also wish to ensure: that each field is the same width; that 1 digit values had leading zeros or spaces consistently; that the separators were consistent; that the spacing was consistent; and so on.

      By the time you've done all that, it might be more straightforward to use a function a convert text dates to numeric dates, which would smooth out format variations for you !

      Tailgating on ikegami's code, here is a more general situation for sorting. This is not applicable here, but if you do enough sorting, it will come up!

      Let's say that for some reason you wanted something different than just an ASCII sort, in this case you lucked out because a simple sort works. But let's say maybe there weren't leading zero's (that makes an ASCII date sort possible!), and say you for some reason wanted sort order, to be month, reverse day, reverse time, here is how that could be accomplished...

      #!/usr/bin/perl use strict; use warnings; my @dates = ( '01-30 22:10', '01-12 05:56', '01-24 01:42', '01-12 05:59', '01-31 01:33', '01-02 01:33', ); @dates = sort @dates; for (@dates) { print $_, "\n"; } #prints: #01-02 01:33 #01-12 05:56 #01-12 05:59 #01-24 01:42 #01-30 22:10 #01-31 01:33 print "\n"; @dates = sort { my ($amonth,$aday,$ahour,$amin) = $a =~/(\d+)-(\d+)\s+(\d+):(\d+)/; my ($bmonth,$bday,$bhour,$bmin) = $b =~/(\d+)-(\d+)\s+(\d+):(\d+)/; $amonth <=> $bmonth or $bday <=> $aday or $bhour <=> $ahour or $bmin <=> $amin }@dates; for (@dates) { print $_, "\n"; } #prints: #01-31 01:33 #01-30 22:10 #01-24 01:42 #01-12 05:59 #01-12 05:56 #01-02 01:33
      The sort function can take a subroutine that you supply! That subroutine produces -1,0,1 depending upon how you figure things should be compared. $a and $b are "magic variables" and will "just be there as part of the sort". The basic procedure is to take the $a and $b variables, use split or regex to break them apart into the things that are relevant for your tricky sort. Then compare them as you wish. My example above shows how I do this with the code layout that I prefer (a clear "stack" of comparisons). For numeric compare, use the "spaceship" operator <=>, for string compare use "cmp". Here I used numeric compares. Notice that what happen in the regex's in sort{} didn't affect the result (they are still just ascii). The list that comes into the sort is what comes out of the sort. You are just supplying a function that compares the lines as the sort routine requests. Note to reverse the order I just swapped the "a" version and the "b" version in the compare functions.

      There are ways to make this computationally more efficient but that requires building lists of anonymous lists (basically do all the splitting,regexing at once instead of doing it each time for the $a and $b variables). Master this type of sorting first.

      Hope this gives an idea of how to solve more complex problems and happy sorting! Perl is great at it!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://740373]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-20 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found