 
 
 
 
To see how to sort an array and use a customized function.
Web Programming book reading for THIS class
JavaScript Array Example 5: Sorting a Simple Array of Random Numbers (Heines)
Web Programming book reading for NEXT class
onchange, onmouseover, onmouseout noscript to Accommodate Disabled JavaScriptQuestions from our last class or the readings or work between classes?
 Math.random() FunctionThis function returns a pseudo-random decimal number between 0 and 1.
    document.writeln( "<ol>" ) ;
for ( var k = 0 ; k < 10 ; k++ ) {
  document.writeln( "<li>" + Math.random() + "</li>" ) ;
}
document.writeln( "</ol>" ) ;
  
    
  
  To generate random numbers greater than 1, we multiply the number generated by 
    the Math.random() function by a number greater than 1. 
    This gives us a range of numbers from 0 to the larger number, such as
     100.
    document.writeln( "<ol>" ) ;
for ( var k = 0 ; k < 10 ; k++ ) {
  document.writeln( "<li>" + ( Math.random() * 100 ) + "</li>" ) ;
}
document.writeln( "</ol>" ) ;
  
    
  
  To generate random integers, we
    again multiply the number generated by the Math.random() function by a
    number greater than 1, but we then pass that result to the
    Math.round() function.
    document.writeln( "<ol>" ) ;
for ( var k = 0 ; k < 10 ; k++ ) {
  document.writeln( "<li>" + Math.round( Math.random() * 100 )  + "</li>" ) ;
}
document.writeln( "</ol>" ) ;
  
    
  
To populate the array, we use the .push() function for each random
    integer generated by the above function.
var arr = new Array() ; // orvar arr = [] ;for ( var k = 0 ; k < 10 ; k++ ) { arr.push( Math.round( Math.random() * 100 ) ) ; }
Using the .sort() function
arr.sort() ;
A comparison function is a function that takes two parameters
    var comp = function( a, b ) { ... }
  
  The function returns a numeric value based on a decision made on those two parameters
    var comp = function( a, b ) { 
    ... // code goes here that decided what number to return
  return number ;
}
  
 a should come before b,
    the function should return a negative number a should come after b,
    the function should return a positive number
    // function to sort numbers in **ascending** order
var comp = function( a, b ) { 
  if ( a < b ) {
    return -1 ;
  } else if ( a > b ) {
    return +1 ;
  } else {
    return 0 ;
  }
}
  
    
  
  Interestingly, you don’t have to return 
    just +1 or -1.  Since 
    any positive and negative number will have the same effect,
    we can simplify the comparison function to:
    // function to sort numbers in **ascending** order
var comp = function( a, b ) { 
  return a - b ;  // note that b is subracted from a in this formula
}
  
   a is 13 and b is 22,
      the function will return -9, which is a negative number, 
      which means that a should come before b a is 22 and b is 13,
      the function will return 9, which is a positive number, 
      which means that a should come after b a and b are both 18,
      the function will return 0 a - b indeed sorts in ascending
      order return b - a ;
    // function to sort numbers in **descending** order
var comp = function( a, b ) { 
  return b - a ;  // note that a is subracted from b in this formula
}
  
    
  
To use a comparison function, you pass it to the sort function
arr.sort( comp ) ;
 a and b !!!Goal: To create and sort a JavaScript array.
Procedure:
 for loop. sort function to sort your array in ascending order. 
    Display your array again to make sure that the sorting works. for loops that you use to display your array should be
      identical. sort function
      should sort your array in alphabetical order. sort function to sort it in ascending order. sort function. <head> section of your HTML file. sort function again, but this time pass it your comparison
    function to sort your array in descending order.  Display your array a third time
    to make sure that the sorting works as expected. 
 
 
 
  This is Class No. 18.  It was last modified on
  Saturday, October 21, 2023 at 4:38 PM.  Copyright © 2010-2025  by Jesse M. Heines.  All rights reserved, but may be freely copied or excerpted for
  educational purposes with credit to the author.