<html> <head> <link rel="stylesheet" type="text/css" href="http://www.greggriffiths.org/style.css"> </head> <body> <div class="content"> <p class="code"> function personRecord(firstName,lastName)<br/> {<br/> this.firstName=firstName;<br/> this.lastName=lastName;<br/> }<br/> <br/> // Create new main array.<br/> var personArray = new Array();<br/> <br/> // set the inital state of the array<br/> personArray[0] = new personRecord("John","Doe");<br/> personArray[1] = new personRecord("Micky","Mouse");<br/> personArray[2] = new personRecord("Minnie","Mouse");<br/> personArray[3] = new personRecord("James","Bond");<br/> personArray[4] = new personRecord("George","Bush");<br/> <br/> function buildIndex()<br/> {<br/> var swapit=0; // do we need to do a swap<br/> <br/> // create some temporary variables to store the data when we do the swapping<br/> var temp_first_name="";<br/> var temp_last_name="";<br/> <br/> // set a local variable equal to the length of the array<br/> var maxPeople=personArray.length;<br/> <br/> // Bubble sort by Last Name alphabetically<br/> for (i=0;i<maxPeople;i++)<br/> {<br/> if ((i+1)<maxPeople)<br/> {<br/> for (j=(i+1);j<maxPeople;j++)<br/> {<br/> swapit=0; // reset the swap flag<br/> <br/> if (personArray[i].lastName > personArray[j].lastName)<br/> {<br/> swapit=1;<br/> }<br/> <br/> // if we need to do the swap<br/> if (swapit==1)<br/> {<br/> temp_first_name=personArray[i].firstName;<br/> temp_last_name=personArray[i].lastName;<br/> personArray[i].firstName=personArray[j].firstName;<br/> personArray[i].lastName=personArray[j].lastName;<br/> personArray[j].firstName=temp_first_name;<br/> personArray[j].lastName=temp_last_name;<br/> }<br/> }<br/> }<br/> }<br/> } </p> </div> </body> </html>