I want to sort an arrayref %results (Time-strings, from old to new), it has multiple keys but I just posted one key to show how it looks like:
'Ende Monatswechsel P-Konten' => [
'17.02.2018 05:17:39',
'14.02.2018 04:28:11',
'23.02.2018 03:17:17',
'22.02.2018 03:39:20',
]
I am expecting:
'Ende Monatswechsel P-Konten' => [
'14.02.2018 04:28:11',
'17.02.2018 05:17:39',
'22.02.2018 03:39:20',
'23.02.2018 03:17:17',
]
Does any know how to do this? I tried:
my $columns = map [ $_, sort{$a <=> $b} @{ $results{$_} } ], keys %results;
but it doesn't work. Thanks in advance.
My code looks like this:
while(my $line=<F>) {
#- Info: 19.02.2018 00:01:01 --- Start Tageswechsel-CoBa ---
#- Info: 27.11.2018 04:16:42 --- Ende Tageswechsel-CoBa ---
if ($line=~ /(\d\d\.\d\d\.\d\d\d\d \d\d:\d\d:\d\d) --- (.+? Tageswechsel-CoBa) -.*\s*$/)
{
($timestamp, $action) = ($1,$2);
}
if ( !defined $filter{$action}{$timestamp} ) {
push @{$results{$action}}, $timestamp;
$filter{$action}{$timestamp} = 1;
}
}
print Dumper(\%results)
outputs:
'Start Tageswechsel-CoBa' => [
'17.02.2018 05:12:13',
'20.02.2018 04:23:16',
'22.02.2018 03:12:46',
'23.02.2018 03:34:28',
'27.02.2018 03:41:25',
'02.03.2018 03:32:26',
],
'Ende Tageswechsel-CoBa' => [
'17.02.2018 05:20:01',
'19.02.2018 06:01:02',
'20.02.2018 04:29:44',
'22.02.2018 03:19:04',
'23.02.2018 03:40:52',
'26.02.2018 06:01:26',
]
};
Something like this would work:
I'm splitting each value into chunks and then sorting them from largest chunk to smallest. Note that as the time is a string, not a number I use
cmp
instead of<=>
.This is slightly inefficient, as I'm re-splitting each data item several times. If that's a problem, then you could look at something like a Schwartzian Transform.
But the best solution to this would be to get a sortable timestamp in the first place. If your dates were
YYYY.MM.DD HH:MM:SS
, then you could just do a simple string sort.Update: My output is
Update 2: I've edited my code to make it more like your example. Hope this helps.
And the output...