Answers for "array obj sort function is not sorting data correctly 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
7

sort array of objects in ascending order in js

homes.sort(function(a, b) {
    return parseFloat(a.price) - parseFloat(b.price);
});
Posted by: Guest on March-17-2020

Code answers related to "array obj sort function is not sorting data correctly javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language