Categories
JavaScript Answers

How to sort an array of integers with JavaScript?

Spread the love

To sort an array of integers with JavaScript, we call the sort method.

For instance, we write

const numArray = [140000, 104, 99];
const sortedNumArray = numArray.sort((a, b) => {
  return a - b;
});

to call sort with a callback that returns a - b to sort the `numArray entries in ascending order in a new array and return it.

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 *