This is a problem you may often face during your development phase: how do you sort this array of objects by the value of a property? Here is a simple solution for you to consider.

Sample input:


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

Sort by single property value


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

Sort by multiple property value


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

0
2023-06-29
JavaScript
0
0
2023-06-29
JavaScript
0
0

Contact Us

If you have any inquiries or feedback, please don't hesitate to reach out to us at [email protected]. We will respond to your request as soon as possible. Thank you very much for your interest!

Country profiles