Answers for "how to sort a array of objects"

26

sort array of object js

const books = [
  {id: 1, name: 'The Lord of the Rings'},
  {id: 2, name: 'A Tale of Two Cities'},
  {id: 3, name: 'Don Quixote'},
  {id: 4, name: 'The Hobbit'}
]

books.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0));
Posted by: Guest on April-08-2021
0

sort objects in an array

cars.sort(function(a, b){return a.year - b.year});
Posted by: Guest on June-02-2021

Code answers related to "how to sort a array of objects"

Code answers related to "Javascript"

Browse Popular Code Answers by Language