Sorting an Array Element



Write a JavaScript code to sort array element in ascending order.

<html>
  <head>
    <title>Array Demo</title>
      </head>
        <script>

          var a = new Array();

            a[0]=20;
            a[1]=13;
            a[2]=5;
            a[3]=2;

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

            for(var i=0;i < a.length;i++)
            {
              document.write(a[i] + "<br>");
            }
            
        </script>
      <body>       
  </body>
</html>

Output:

2
5
13
20