Answers for "how to sort ojects inside array"

2

sort array with objects

const list = [
  { color: 'white', size: 'XXL' },
  { color: 'red', size: 'XL' },
  { color: 'black', size: 'M' }
]

var sortedArray = list.sort((a, b) => (a.color > b.color) ? 1 : -1)

// Result:
//sortedArray:
//{ color: 'black', size: 'M' }
//{ color: 'red', size: 'XL' }
//{ color: 'white', size: 'XXL' }
Posted by: Guest on June-06-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 ojects inside array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language