Created
February 15, 2021 17:16
-
-
Save petsel/bc8298ae141b0d30aab778d319bd9011 to your computer and use it in GitHub Desktop.
provides a `localeCompare` based compare function which can be used like `[/* ... */].sort(localeCompare)`
This file contains 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
function defaultCompare(a, b) { | |
return ((a < b) && -1) || ((a > b) && 1) || 0; | |
} | |
// function localeCompare(a, b) { | |
// return a.localeCompare | |
// ? a.localeCompare(b) | |
// : defaultCompare(a, b); | |
// } | |
// - supports additional `…[, locales[, options]` argument binding | |
// - to be used like … | |
// … [const lc =] localeCompare.bind(null, 'de', { sensitivity: 'base' }); | |
// … [const lc =] localeCompare.bind(...[, 'de', { sensitivity: 'base' }]); | |
// | |
// - or to be used like before, plainly as with … | |
// … [/* ... */].sort(localeCompare); | |
// | |
function localeCompare(...args) { | |
const [a, b] = args.splice(args.length - 2, 2); | |
return a.localeCompare | |
? a.localeCompare(b, ...args) | |
: defaultCompare(a, b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment