Answers for "sort in javascript array of objects by 1.1"

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 object with certain value at start of array js

objs.sort((a,b) => {
 	if (a.value == "your value") return parseInt("-" + objs.indexOf(a)) // moves up based on its index
})
Posted by: Guest on July-22-2021

Code answers related to "sort in javascript array of objects by 1.1"

Code answers related to "Javascript"

Browse Popular Code Answers by Language