Skip to content

Instantly share code, notes, and snippets.

@rossja
Created November 11, 2021 17:55
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 rossja/8eaed9e7aac49f4d7a8c5c035b840e52 to your computer and use it in GitHub Desktop.
Save rossja/8eaed9e7aac49f4d7a8c5c035b840e52 to your computer and use it in GitHub Desktop.
/**
* utils module - provides various utility functions used by the application
* @module utils
*/
/**
* A function to sort an array of indices by a given property
* put simply: this lets you sort an array of json objects by
* whatever property you tell it to use.
* Example: `var teams = data.teams.sort(utils.GetSortOrder("name"))`
* @param {string} prop - an index property name
* @returns {function} - a function to sort an array of indices by the given property
*/
exports.GetSortOrder = function(prop) {
return function(a, b) {
if (a[prop] > b[prop]) {
return 1;
} else if (a[prop] < b[prop]) {
return -1;
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment