js array sort based on key in object code example
Example 1: sort array of objects javascript
list.sort((a, b) => (a.color > b.color) ? 1 : -1)
Example 2: array sort by key javascript
function sortByKey(array, key) {
return array.sort((a, b) => {
let x = a[key];
let y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}