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));





Discover More
Javascript - Array

array in Javascript can contain any type of data. Different types of values can be stored within the same array Because arrays are special objects (as typeof implies), they can also have properties, including...



Share this page:
Follow us:
Task Runner