That's why the sorted record I built has three parameters, normalized date, original date string, and a hash key. You retrieve the rest of the data with the hash key, which is
$_->[2]. And also remember that how to optimize the storage/retrieval depends on what sort of output is required.
The code is actually quite simple, really. The sorting part is based on the Schwartzian Transform, invented by
merlyn.
The following is the explanation of the sort, hope you might find it useful. ;-)
my @sorted_date =
map { [ $_->[1], $_->[0], $_->[2] ] }
sort { $b->[1] <=> $a->[1] }
map { my @n = $list{$_} =~ /^(\d+)\.(\d+)\.(\d+):/;
my $d = sprintf "%02d%02d%02d", @n[2,0,1];
[join('.',@n), $d, $_] } keys %list;
step 1: build a list of hash keys: keys %list;
step 2: build a new array by:
for each element of the list built in step 1,
capture the MM, DD and YY values into @n
with the @ary = $str =~ /pattern/ idiom,
normalize the date into YYMMDD form,
store the original date, normalized date and
hash key in anonymous array
step 3: do the sort on normalized dates
step 4: just reshuffle the fields in sorted data.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.