Answers for "sort object of objects 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
5

sort array of objects javascript

list.sort((a, b) => (a.color > b.color) ? 1 : -1)
Posted by: Guest on July-23-2020
1

javascript sort object js

grossaryList = {
  'bread': 1,
  'apple': 6,
  'milk': 1, 
  'orange': 3,
  'broccoli': 2 
}

return Object
  .entries(grossaryList)
  .sort((a,b) => b[1]-a[1])

//=> [['apple', 6],['orange', 3],['broccoli', 2],['bread',1],['milk', 1]]
Posted by: Guest on April-27-2021
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
3

javascript sort object

var maxSpeed = {
    car: 300, 
    bike: 60, 
    motorbike: 200, 
    airplane: 1000,
    helicopter: 400, 
    rocket: 28800
};
var sortable = [];
for (var vehicle in maxSpeed) {
    sortable.push([vehicle, maxSpeed[vehicle]]);
}

sortable.sort(function(a, b) {
    return a[1] - b[1];
});

//[["bike", 60], ["motorbike", 200], ["car", 300],
//["helicopter", 400], ["airplane", 1000], ["rocket", 28800]]
Posted by: Guest on April-19-2020

Code answers related to "sort object of objects javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language