How to Sort Numeric Values in Arrays

Published on: October 1, 2016

Welcome to the fifteenth Easy JavaScript Tutorial, Beginner JavaScript Tutorial, part of Easy Programming!  In the last Tutorial, we covered many array methods. At the end, we looked at the sort() method and I mentioned that this method can get quirky when dealing with numeric values in your array. 


In this tutorial, we're going to look at what happens when you try to sort numeric values and how you can get around it. 


Sorting in Ascending Order:

    function(a,b){return a-b}

Sorting in Descending Order:

    function(a,b){return b-a}

To fork the fiddle and follow along:

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!