Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gregogalante
Last active August 30, 2018 10:33
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 gregogalante/0536d5a8c31f1fbd7bbe3e91bdd69019 to your computer and use it in GitHub Desktop.
Save gregogalante/0536d5a8c31f1fbd7bbe3e91bdd69019 to your computer and use it in GitHub Desktop.
CustomArraySort.js
/**
* @param {array} array
* @param {string} attribute
* @param {array} sortOrder
*/
function customArraySort (array, attribute, sortOrder) {
let ordering = {}
for (let i = 0; i < sortOrder.length; i++) {
ordering[sortOrder[i]] = i
}
array.sort( function(a, b) {
return (ordering[a[attribute]] - ordering[b[attribute]]) || a[attribute].localeCompare(b[attribute])
})
return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment