Website Development I

Course Info Class Notes Resources Student Websites

Class No. 18:

Structuring and Sorting Data Stored in Arrays

Today’s Goal

To see how to sort an array and use a customized function.


Class Resources

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


Preambles

Questions from our last class or the readings or work between classes?


Creating a Simple Array of Random Numbers

The Math.random() Function

This 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>" ) ;

Populating the Array

To populate the array, we use the .push() function for each random integer generated by the above function.

var arr = new Array() ;  // or var arr = [] ;
for ( var k = 0 ; k < 10 ; k++ ) {
  arr.push( Math.round( Math.random() * 100 ) ) ;
}

Sorting the Array

Using the .sort() function

arr.sort() ;

Writing a Comparison Function

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 ;
}
// 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
}
// 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
}

Using a Comparison Function

To use a comparison function, you pass it to the sort function

arr.sort( comp ) ;


Today’s Exercise

Goal:  To create and sort a JavaScript array.

Procedure:

  1. Start by working with one of the arrays that you created for our last class.  I recommend that you copy the file you were working on to a new file so that you don’t lose your previous work.
  2. Make sure that you can still display your original array using a for loop.
  3. Use the sort function to sort your array in ascending order.  Display your array again to make sure that the sorting works.
  4. Write a comparison function to sort your array in descending order.
  5. Use the 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-2024 by Jesse M. Heines.  All rights reserved, but may be freely copied or excerpted for educational purposes with credit to the author.