Skip to content

Instantly share code, notes, and snippets.

@patarapolw
Created April 3, 2019 10:09
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 patarapolw/6182d90d21b9dfffd6ecf0495bfe554a to your computer and use it in GitHub Desktop.
Save patarapolw/6182d90d21b9dfffd6ecf0495bfe554a to your computer and use it in GitHub Desktop.
Javascript sort
a.sort((a, b) => {
function convert(x: any) {
return x[sortBy];
}
function compare() {
const m = convert(a) || -Infinity;
const n = convert(b) || -Infinity;
if (typeof m === "string" && typeof n === "string") {
return m.localeCompare(n);
} else if (typeof m === "string") {
return 1;
} else if (typeof n === "string") {
return -1;
} else {
return m - n;
}
}
return desc ? -compare() : compare();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment