Answers for "sort data in object array javascript"

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 "sort data in object array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language