Answers for "array of objects sort 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
19

javascript sort array with objects

var array = [
  {name: "John", age: 34},
  {name: "Peter", age: 54},
  {name: "Jake", age: 25}
];

array.sort(function(a, b) {
  return a.age - b.age;
}); // Sort youngest first
Posted by: Guest on May-20-2020
17

javascript sort array of objects

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'}
]

compareObjects(object1, object2, key) {
  const obj1 = object1[key].toUpperCase()
  const obj2 = object2[key].toUpperCase()

  if (obj1 < obj2) {
    return -1
  }
  if (obj1 > obj2) {
    return 1
  }
  return 0
}

books.sort((book1, book2) => {
  return compareObjects(book1, book2, 'name')
})

// Result:
// {id: 2, name: 'A Tale of Two Cities'}
// {id: 3, name: 'Don Quixote'}
// {id: 4, name: 'The Hobbit'}
// {id: 1, name: 'The Lord of the Rings'}
Posted by: Guest on February-25-2020
0

js sort array of objects

const drinks1 = [
	{name: 'lemonade', price: 90}, 
	{name: 'lime', price: 432}, 
	{name: 'peach', price: 23}
];

function sortDrinkByPrice(drinks) {
	return drinks.sort((a, b) => a.price - b.price);
}
Posted by: Guest on February-19-2021
7

sort array of objects javascript by value

let orders = [
  { 
    order: 'order 1', date: '2020/04/01_11:09:05'
  },
  { 
    order: 'order 2', date: '2020/04/01_10:29:35'
  },
  { 
    order: 'order 3', date: '2020/04/01_10:28:44'
  }
];


console.log(orders);

orders.sort(function(a, b){
  let dateA = a.date.toLowerCase();
  let dateB = b.date.toLowerCase();
  if (dateA < dateB) 
  {
    return -1;
  }    
  else if (dateA > dateB)
  {
    return 1;
  }   
  return 0;
});

console.log(orders);
Posted by: Guest on April-01-2020
0

javascript sort array of objects

// Handles mix-cased strings and numbers with option for setting order
const sortObjectArray = ({ arr, field, order = 'desc' }) => {
   arr.sort(function(a, b) {
      const fieldA = typeof a[field] === 'string' ? a[field].toLowerCase() : a[field]
      const fieldB = typeof b[field] === 'string' ? b[field].toLowerCase() : b[field]

      let result
      if (order === 'desc') {
         result = fieldA > fieldB ? 1 : -1
      } else {
         result = fieldA < fieldB ? 1 : -1
      }
      return result
   })
   return arr
}

let arr = [
  {name: "John", age: 34},
  {name: "Peter", age: 54},
  {name: "jake", age: 25}
]

let arr2 = sortObjectArray({ arr, field: 'name'})
console.log(arr2)
/*
[
{ age: 25, name: "jake"}, 
{ age: 34, name: "John" }, 
{ age: 54, name: "Peter" }
]
*/

let arr3 = sortObjectArray({ arr, field: 'age', order: 'asc' })
console.log(arr3)
/*
[
{ age: 54, name: "Peter" },
{ age: 34, name: "John" }, 
{ age: 25, name: "jake"}
]
*/
Posted by: Guest on March-25-2021

Code answers related to "array of objects sort javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language