Skip to content

Instantly share code, notes, and snippets.

@petsel
Created February 15, 2021 17:16
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 petsel/bc8298ae141b0d30aab778d319bd9011 to your computer and use it in GitHub Desktop.
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)`
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