|
|
|
Your skill will accomplish what the force of many cannot |
|
| PerlMonks |
Re: Get most recent data based on a date from an array of hashes.by davido (Cardinal) |
| on Jan 18, 2022 at 20:54 UTC ( [id://11140585]=note: print w/replies, xml ) | Need Help?? |
|
I would approach it by transforming the date field into ISO8601 format, holding onto it as a sort key, and then sorting based on that format. Here's one way of doing that:
My own paranoia about date manipulation is that I should be using a module for date comparisons and transformations, so this approach sort of goes against my own instinct to not try to do date manipulation myself. If there's any possibility at all that dates aren't as easy to transform as I've done here, use a module for that. This approach uses a Schwartzian Transform to augment the data we're sorting with a sort key. We create a tuple of [$sort_key, $payload], sort those tuples, then retain only the payload from the sorted tuples. And this approach defines a to_iso8601 function that converts mm-dd-yyyy format (or even m-d-yyyy) to yyyy-mm-dd format to use as a sort key. Dave
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||||||