Sort array of objects by string property value

Created by WoganLinked to 26.9m issues across 300 teams


For example, given an array of objects with a name property, we can sort the array in ascending order by the name property value using the Array.prototype.sort() method:

let arr = [ {name: 'John'}, {name: 'Adam'}, {name: 'Zoe'}, {name: 'Jane'} ]; arr.sort((a, b) => ( > ? 1 : -1); console.log(arr); // Output: [{name: 'Adam'}, {name: 'Jane'}, {name: 'John'}, {name: 'Zoe'}]