We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Pure Functions Practice

Datetimes are infamously a pain in the neck for programming. One of the easier items on the long list of problems is the order of the year, month, and day in a calendar date. Most of the world uses the day-month-year format, but some insane countries use month-day-year because they want everyone else to be miserable.

Assignment

Refactor the sort_dates function. It currently uses .sort(), which modifies the list in-place (an impure operation). We want to change it to use sorted() with a custom helper function, to sort the dates chronologically without modifying the original list.

When you pass key=format_date, Python runs your helper function on every item to figure out the sort order (using the YYYYMMDD format), but it puts the original dates (MM-DD-YYYY) into the final list.