objArr.sort(function (a, b) { if (a.key < b.key) return -1 if (a.key > b.key) return 1 return 0 }) // or objArr.sort(function (a, b) { if (a['key'] < b['key']) return -1 if (a['key'] > b['key']) return 1 return 0 }) const users = [ { name: 'Asabeneh', age: 150 }, { name: 'Brook', age: 50 }, { name: 'Eyob', age: 100 }, { name: 'Elias', age: 22 }, ] users.sort((a, b) => { if (a.age < b.age) return -1 if (a.age > b.age) return 1 return 0 }) console.log(users) // sorted ascending // [{…}, {…}, {…}, {…}]
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter