I got an array (see below for one object in the array) that I need to sort by firstname using JavaScript. How can I do it?
var user = {
bio: null,
emai
I'm surprised no one mentioned Collators. You shouldn't use localeCompare unless you have to as it has significantly worse performance.
const collator = new Intl.Collator('zh-CN', { // Chinese Simplified for example
numeric: true,
sensitivity: 'base',
});
function sortAsc(a, b) {
if (typeof a === 'string' && typeof b === 'string') {
return collator.compare(b, a)
}
return b - a;
}
function sortDesc(a, b) {
if (typeof a === 'string' && typeof b === 'string') {
return collator.compare(a, b);
}
return a - b;
}