The purpose of this script is to demonstrate a sort function (@26-32) that reorders an array (dates => sortedDates
) based on proximity to a specified date (search
and d3o
), with a bias towards future dates.
Use case includes searching for a history-related item in the past given a date. This functions serves to find the closest item relative to a date resolved from speech. For example, it is the assumption that when a user requests an item by date using "Last Tuesday", where no item exists on the date resolved from "Last Tuesday", that the user's bias will be towards the closest date more future to the one specified. This is based off of the additional assumption that the requester's memory is more likely to mistake "Wednesday" for "Tuesday", rather than "Monday" for "Tuesday" because "Wednesday" is closer to the current date, and thus their memory of it was likely to be more vivid.
These assumptions are not supported by any studies or scientific facts.
Additionally, when implementing this algorithm it is important to understand that the user is more likely to have meant "Tuesday" than either "Wednesday" or "Monday". Because of this, any date that does not specifically match the user's originally requested date should be verified in the form of a suggestion.
i.e., This sorting logic should return a list of suggestions after a search for the exact date fails, and a clean user experience would dictate responding to the user with a follow-up question for verification (e.g., "Did you mean Wednesday the 27th?").
Note that adding the search
date to the dates
array will result in the search
date as the zeroth element in the resulting array (sortedDates
).
Golf'd: