How to sort array of numbers in javascript

By default, Javascript sorts an array in a lexicographical way.

To sort numeric array, provide a comparison function. Use the code below:

myArray.sort(function(a,b) { return a – b; } );

Leave a Reply

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