Categories
JavaScript Answers

How to sort JavaScript array by two numeric fields?

Spread the love

To sort JavaScript array by two numeric fields, we call the sort method.

For instance, we write

const sorted = grouperArray.sort((a, b) => {
  return a.size - b.size || a.glow - b.glow;
});

to call sort with a callback that compares the items by their size property values and glow property values.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *