As in our previous example of sorting array elements alphabetically we have used the default sort method to sort array elements but here in this example of sorting array elements ( numeric elements ) we have created a sortNumbers() function which sorts the numbers.
In this example we can see that if we use the default sort() method on the numbers it doesn't sort these numbers correctly. Since it takes these numeric elements as alphabet and therefore we need to define a sorting function for sorting numbers. Given below is the full example code of this example html page:
var arr = new Array(5);
document.write("<b>Original Array is </b>=>"+arr+"<br>");
document.write("<b>Sorted array without calling any function is</b>=>"
document.write("<b>Sorted array after calling sortNumbers function is
Implementing sort() method on numbers
By running this example on your browser you will get the following output:
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.