javascript sort array of objects alphabetically code example
Example 1: javascript sort alphabetically
var items = ['réservé', 'premier', 'communiqué', 'café', 'adieu', 'éclair'];
items.sort(function (a, b) {
return a.localeCompare(b);
});
Example 2: sort object alphabetically javascript
let objArray= [
{v1:"Bee", v2: 2},
{v1:"Apple", v2: 8},
{v1:"bat", v2: 4},
];
let sortedArray= objArray.sort(function(a, b) {
return a.v1.localeCompare(b.v1);
});
Example 3: array sort by alphabetical javascript
users.sort((a, b) => a.firstname.localeCompare(b.firstname))