#cat unsorteddates.csv
2016-02-05:00:39,mttsmshub1
2005-02-02:00:44,mttsmshub1
2012-02-03:00:32,tttsmshub1
2013-02-04:00:24,mttsmshub1
2000-04-02:00:44,mttsmshub1
For your convenience you can convert your dates to epoch times using, for example, Date::Parse ( but see Re: Date to epoch ) after having
chomped strings from file and having splitted their content. You'll feed with this map block the @unsorted array with anonymous arrays containing as 0th element the epoch and as 1st one the other string untouched.
The resulting datastructure will be:
(
[1454629140, "mttsmshub1"],
[1107301440, "mttsmshub1"],
[1328225520, "tttsmshub1"],
[1359933840, "mttsmshub1"],
[954629040, "mttsmshub1"],
)
Then in a second block you sort by the 0th field of each anonymous array of @unsorted (ie epochs) but before printing you reconvert back epochs to strings (i've used scalar localtime ($$_[0]) for lazyness: choose the appropriate format)
use strict;
use warnings;
use Date::Parse;
open my $csv,'<','unsorteddates.csv' or die "Unable to open for read";
my @unsorted = map {
chomp;
my @parts = split ',',$_;
[ str2time($parts[0]),$parts[1] ]
} <$csv>;
print join "\n", map {scalar localtime ($$_[0]).",$$_[1]"}
sort {$$a[0] <=> $$b[0]} @unsorted;
# output
Sun Apr 2 00:44:00 2000,mttsmshub1
Wed Feb 2 00:44:00 2005,mttsmshub1
Fri Feb 3 00:32:00 2012,tttsmshub1
Mon Feb 4 00:24:00 2013,mttsmshub1
Fri Feb 5 00:39:00 2016,mttsmshub1
HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
|