Sort-2

 

arr.sort() function accept function as optional argument for sorting. Here is how it sort the array in asc order.

arr.sort(function(a, b) { return a - b; })

 
 
 
<!DOCTYPE html> <html> <head> <title>Welcome to LearnKode - A code learning platform</title> </head> <body> <script> var arr= [4, 2, 5, 1, 3]; arr.sort(function(a, b) { return a - b; }); document.write(arr); </script> </body> </html>
Output