<%
OPTION EXPLICIT

Dim dbConn, MdbFilePath, SQL_query, results

Set dbConn = Server.CreateObject("ADODB.Connection")
MdbFilePath = Server.MapPath("table_article.mdb")
dbConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
SQL_query = "SELECT * FROM tbl_people"

if (Request.QueryString("sort")) then
	SQL_query=SQL_query & " ORDER BY lastname"
end if

Set results = dbConn.Execute(SQL_query)

%>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="http://www.greggriffiths.org/style.css">

	<script language="javascript" type="text/javascript">
		function personRecord(firstName,lastName)
		{
			this.firstName=firstName;
			this.lastName=lastName;
		}

		// Create new main array.
		var personArray = new Array();

		<%
			Dim count
			count=0

			do while not results.EOF

				response.write("personArray[" & count & "] = new personRecord('" & results("firstname") & "','" & results("lastname") & "');")

				count=count+1

				results.movenext
			loop
		%>

		function buildIndex()
		{
			var swapit=0; // do we need to do a swap

			// create some temporary variables to store the data when we do the swapping
			var temp_first_name="";
			var temp_last_name="";

			// set a local variable equal to the length of the array
			var maxPeople=personArray.length;

			// Bubble sort by Last Name alphabetically
			for (i=0;i<maxPeople;i++)
			{
				if ((i+1)<maxPeople)
				{
					for (j=(i+1);j<maxPeople;j++)
					{
						swapit=0; // reset the swap flag

						if (personArray[i].lastName > personArray[j].lastName)
						{
							swapit=1;
						}

						// if we need to do the swap
						if (swapit==1)
						{
							temp_first_name=personArray[i].firstName;
							temp_last_name=personArray[i].lastName;
							personArray[i].firstName=personArray[j].firstName;
							personArray[i].lastName=personArray[j].lastName;
							personArray[j].firstName=temp_first_name;
							personArray[j].lastName=temp_last_name;
						}
					}
				}
			}
		}

		function printTable()
		{
			for  (i=0;i<personArray.length;i++)
			{
				document.write("<tr>");
				document.write("	<td>" + personArray[i].firstName + "</td>");
				document.write("	<td>" + personArray[i].lastName + "</td>");
				document.write("</tr>");
			}
		}

		function processQS(field2find)
		{
			// check for Parameters
			if (document.location.search.toString().length>0)
			{
				var QS = document.location.search.substring(1);

				var argname=null;
				var argvalue=null;
				var pairs = QS.split('&');

				for (i=0;i<pairs.length;i++)
				{
					var pos=pairs[i].indexOf('=');

					if (pos!=-1)
					{
						var argname=pairs[i].substring(0,pos);
						var argvalue=pairs[i].substring(pos+1);

						if (argname.toLowerCase()==field2find.toString().toLowerCase())
						{
							return(unescape(argvalue));
						}
					}
				}

				// if we get here we have no match
				return "";
			}
		}

		if (processQS("sorted"))
		{
			buildIndex();
		}

	</script>

</head>
<body>
	<table class="default">
		<tr>
			<th>First Name</th>
			<th>Last Name</th>

		</tr>
		<script language="javascript" type="text/javascript">
			printTable();
		</script>
	</table>
	<p>
	Sort <a href="demo10.asp?sorted=true">Alphabetically</a><br/>

	<a href="demo10.asp">Return to Initial order</a>
</body>
</html>