Sorting for Database Forms with HTML - The getColumnIndex() Function
(Page 3 of 4 )
This function returns the column index. It takes the title of the recordset column as an argument. The titles for the columns are ID, Name, Department, Job, Years, and Salary. The index it returns is a value from 0 to 5. This is the function:
function getColumnIndex(title)
{
for (i=0; i<6; i++)
{
if(titleBoolArr[i][0] == title)
return i;
}
return 6;
}
The titleBoolArr array in the function has been designed in such a way that the recordset column index is the row index of the titleBoolArr array. The above function is self-explanatory.
The quickSort() Function
This is the function that actually does the sorting. It receives the first row index (0) of the recordset, the last row index (numberOfRows - 1) of the recordset, and the column index (titleInx) for a column in the recordset as parameters. The sorting takes place based on a column (titleInx). The quickSort() function does not return any value; it rearranges the rows of the recordset in ascending order based on a column. As I said above, if you want to understand how the function operates, read one of my previous articles, titled “Quicksort.” However, this is the function:
function quickSort(left,right,titleInx)
{//the function receives a recordset, indices for the left and right rows, and the index for the title column.
i = left;
j = right;
if(document.getElementById('Recordset').rows[i].cells[titleInx].innerHTML > document.getElementById('Recordset').rows[j].cells[titleInx].innerHTML)
{
swap(i, j);
}
do //increment i and decrement j, then swap if necessary
{
do
{
if ((i+1)<=right) //do not increment i beyond the index for the right element
{
++i;
}
} while (document.getElementById('Recordset').rows[i].cells[titleInx].innerHTML < document.getElementById('Recordset').rows[left].cells[titleInx].innerHTML)
do
{
if ((j-1)>=left) //do not decrement j beyond the index for the left element
{
--j;
}
} while (document.getElementById('Recordset').rows[j].cells[titleInx].innerHTML > document.getElementById('Recordset').rows[left].cells[titleInx].innerHTML)
if (i<j)
{
swap(i, j);
}
} while (i < j)
swap(j, left);
var k = j; //the pivot.
var leftLeft = left; //the index for the left element of the left sub-list, initialized
if ((k-1)>=left) //we do not want a value for k that will be less than the left index
{
var leftRight = k - 1; //the index for the right element of the left sub-list, initialized
}
if ((k+1)<=right) //we do not want a value for k that will be greater than the right index
{
var rightLeft = k + 1; //the index for the left element of the right sub-list, initialized
}
var rightRight = right; //the index for the right element of the right sub-list, initialized
//recursion for the left sub-list - only called if a left sub-list exists
if (k != left)
{
quickSort(leftLeft,leftRight,titleInx);
}
//recursion for the right sub-list - only called if a right sub-list exists
if (k != right)
{
quickSort(rightLeft,rightRight,titleInx);
}
}
The recordset is like a two dimensional array. The quickSort() function is slightly different from the one I wrote in the article titled, "Quicksort." In that article, sorting is done only for a one-dimensional array. Here, the function is different, because the two dimensional array aspects have to be taken into consideration.
Note that after the sorting is done, the result stays in memory. If you exit the program and start again the resultset will be in its original state.
There is a corresponding function to quickSort(), called swap(). The swap() function takes in two parameters that are the indices of the recordset rows to be swapped. It swaps the contents of the two rows. This is the function:
function swap(m,n)
{
for (z=0; z<7; z++)
{
temp = document.getElementById('Recordset').rows[m].cells[z].innerHTML;
document.getElementById('Recordset').rows[m].cells[z].innerHTML = document.getElementById('Recordset').rows[n].cells[z].innerHTML;
document.getElementById('Recordset').rows[n].cells[z].innerHTML = temp;
}
}
Next: The titleBoolArr Array >>
More HTML Articles
More By Chrysanthus Forcha