How TO - Sort Numeric Array


Learn how to sort an array numerically in JavaScript.


Sort an Array Numerically

You can use the folllowing to sort an array numerically:

Example

const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
Try it Yourself »

You can also sort the array descending:

Example

const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
Try it Yourself »

Read more about how to sort arrays in our JavaScript Sorting Arrays Tutorial.


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.