Advertisement
RokiAdhytama

Sort - Chapter 11

Jul 4th, 2022
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html xmlns = "http://www.w3.org/1999/xhtml">
  2.    <head>
  3.       <title>Sorting an Array with Array Method sort</title>  
  4.  
  5.       <script type = "text/javascript">
  6.          <!--
  7.          function start()
  8.          {
  9.             var a = [ 10, 1, 9, 2, 8, 3, 7, 4, 6, 5 ];
  10.  
  11.             document.writeln( "<h1>Sorting an Array</h1>" );
  12.             outputArray( "Data items in original order: ", a );
  13.             a.sort( compareIntegers );  // sort the array
  14.             outputArray( "Data items in ascending order: ", a );
  15.          }
  16.    
  17.          // outputs "header" followed by the contents of "theArray"
  18.          function outputArray( header, theArray )
  19.          {
  20.             document.writeln( "<p>" + header +  
  21.                theArray.join( " " ) + "</p>" );  
  22.          }
  23.    
  24.          // comparison function for use with sort
  25.          function compareIntegers( value1, value2 )
  26.          {
  27.             return parseInt( value1 ) - parseInt( value2 );  
  28.          }
  29.          // -->
  30.       </script>
  31.  
  32.    </head><body onload = "start()"></body>
  33. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement