Last active
August 30, 2018 10:33
-
-
Save gregogalante/0536d5a8c31f1fbd7bbe3e91bdd69019 to your computer and use it in GitHub Desktop.
CustomArraySort.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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