Javascript Order by String Array
users.sort((a, b) => a.firstname.localeCompare(b.firstname))
Motionless Macaque
users.sort((a, b) => a.firstname.localeCompare(b.firstname))
// function that returns a string in alphabetical order
function reorderStr(str) {
return [...str].sort().join('');
}
console.log(reorderStr("hacker")); //"acehkr"
console.log(reorderStr("javascript"));//"aacijprstv"
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]
var names = ["Peter", "Emma", "Jack", "Mia"];
var sorted = names.sort(); // ["Emma", "Jack", "Mia", "Peter"]