How to sort an array of objects in Javascript?

How to sort an array of objects in Javascript?

About

This example shows you how you can sort an array of objects with the sort function

Example

let colors = [
  { name: 'blue' },
  { name: 'red' },
  { name: 'purple' },
];
colors.sort((a, b) => a.name.localeCompare(b.name));
colors.forEach(c=>console.log(c.name));







Share this page:
Follow us:
Task Runner