javascript sort array by key name code example
Example: 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));
});
}