Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html xmlns = "http://www.w3.org/1999/xhtml">
- <head>
- <title>Sorting an Array with Array Method sort</title>
- <script type = "text/javascript">
- <!--
- function start()
- {
- var a = [ 10, 1, 9, 2, 8, 3, 7, 4, 6, 5 ];
- document.writeln( "<h1>Sorting an Array</h1>" );
- outputArray( "Data items in original order: ", a );
- a.sort( compareIntegers ); // sort the array
- outputArray( "Data items in ascending order: ", a );
- }
- // outputs "header" followed by the contents of "theArray"
- function outputArray( header, theArray )
- {
- document.writeln( "<p>" + header +
- theArray.join( " " ) + "</p>" );
- }
- // comparison function for use with sort
- function compareIntegers( value1, value2 )
- {
- return parseInt( value1 ) - parseInt( value2 );
- }
- // -->
- </script>
- </head><body onload = "start()"></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement