### Re^2: Sort by Date and Time

by zod (Scribe)
 on Jan 31, 2009 at 03:22 UTC ( #740376=note: print w/replies, xml ) Need Help??

in reply to Re: Sort by Date and Time
in thread Sort by Date and Time

Hmm. I assumed I had to convert dates to datetimes. I knew it looked wrong, though. thanks

Replies are listed 'Best First'.
Re^3: Sort by Date and Time
by ikegami (Pope) on Jan 31, 2009 at 03:23 UTC
Generally, yes. In this case, they're already in a sortable format. The fields are fixed width and they are ordered by decreasing significance.
Re^3: Sort by Date and Time
by gone2015 (Deacon) on Feb 01, 2009 at 02:20 UTC

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 !

Re^3: Sort by Date and Time
by Marshall (Abbot) on Feb 01, 2009 at 14:54 UTC
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
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!

Create A New User
Node Status?
node history
Node Type: note [id://740376]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-08-21 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (325 votes). Check out past polls.

Notices?