sort array with objects on one property code example
Example 1: sort by object property javascript
let list = [
{
name: "world"
},
{
name: "hello",
},
];
// This doesn't account for if names are the same between objects
let x = list.sort((a, b) => (a.name > b.name ? 1 : -1));
console.log(x);
/*
[
{
name: "hello",
},
{
name: "world"
},
];
*/
Example 2: how to sort an array of objects by a property value in javascript
let x = list.sort((a, b) => (a.name > b.name ? 1 : -1));