Array Sorting Example 1

First companion example to my post on sorting JavaScript arrays.

by Lance Leonard

HTML

<p>Here are some book titles:</p>

<div id="pOutput"></div>

CSS

#pOutput { 
  white-space : pre;
  margin-left: 1em;
}

JavaScript

function writeTitle( strTitle ) {
    var obj = document.getElementById( "pOutput" );
    obj.textContent += strTitle + "\n";
}

var astrTitles = [
    "A Study in Scarlet",
    "The Sign of the Four",
    "The Adventures of Sherlock Holmes",
    "The Memoirs of Sherlock Holmes",
    "The Hound of the Baskervilles",
    "The Return of Sherlock Holmes",
    "His Last Bow",
    "The Valley of Fear",
    "The Case-Book of Sherlock Holmes"
];

var iTotal = astrTitles.length; 
if ( iTotal > 0 ) {
   
   for ( var iTitles = 0; iTitles < iTotal; iTitles++ ) {
      var str = astrTitles[ iTitles ];
      writeTitle( str );
   }
}