Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swivelgames/723d8e7caaf96a4c016509e87699de73 to your computer and use it in GitHub Desktop.
Save Swivelgames/723d8e7caaf96a4c016509e87699de73 to your computer and use it in GitHub Desktop.
Sorting Results By Date Relevance, Bias Towards Future

Sorting Results By Date Relevance, Bias Towards Future

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).

var dates = [
"September 28, 2017",
"September 27, 2017",
"September 26, 2017",
"September 25, 2017",
"September 23, 2017",
"September 22, 2017",
"September 21, 2017",
"September 20, 2017"
];
var expectedDates = [
"September 25, 2017",
"September 23, 2017",
"September 26, 2017",
"September 22, 2017",
"September 27, 2017",
"September 21, 2017",
"September 28, 2017",
"September 20, 2017"
];
var search = "September 24, 2017";
var d3o = new Date(search);
var sortedDates = dates.sort((d1, d2) => {
var diff1 = (new Date(d1)) - d3o;
var diff2 = (new Date(d2)) - d3o;
if (Math.abs(diff1) > Math.abs(diff2)) return 1;
if (Math.abs(diff1) < Math.abs(diff2)) return -1;
return 0;
});
console.log({
sortedDates,
matches: sortedDates.reduce(
(b,v,i) => (!b) ? b : v === expectedDates[i],
true
)
});
@Swivelgames
Copy link
Author

Sample output:

{ sortedDates: 
   [ 'September 25, 2017',
     'September 23, 2017',
     'September 26, 2017',
     'September 22, 2017',
     'September 27, 2017',
     'September 21, 2017',
     'September 28, 2017',
     'September 20, 2017' ],
  matches: true }

@Swivelgames
Copy link
Author

Swivelgames commented Sep 26, 2017

Golf'd:

var sortedDates = dates.sort((d1, d2) => Math.abs((new Date(d1)) - d3o) - Math.abs((new Date(d2)) - d3o));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment