Table Sorting Scripts & Example
Table Sorting Scripts & Example
<!-- step one: paste this code into the head of your html document -->
<head>
<script language="javascript">
<!-- this script and many more are available free online at -->
<!-- the javascript source!! http://javascript.internet.com -->
<!-- begin
function setdatatype(cvalue)
{
// this function converts dates and numbers for proper array
// sorting when in the sort function
var isdate = new date(cvalue);
if (isdate == "nan")
{
if (isnan(cvalue))
{
// the value is a string, make all characters in
// string upper case to assure proper a-z sort
cvalue = cvalue.touppercase();
return cvalue;
}
else
{
// value is a number, to prevent string sorting of a number
// add an additional digit that is the + to the length of
// the number when it is a string
var mynum;
mynum = string.fromcharcode(48 + cvalue.length) + cvalue;
return mynum;
}
}
else
{
// value to sort is a date, remove all of the punctuation and
// and return the string number
//bug - string and not numerical sort .....
// ( 1 - 10 - 11 - 2 - 3 - 4 - 41 - 5 etc.)
var mydate = new string();
mydate = isdate.getfullyear() + " " ;
mydate = mydate + isdate.getmonth() + " ";
mydate = mydate + isdate.getdate(); + " ";
mydate = mydate + isdate.gethours(); + " ";
mydate = mydate + isdate.getminutes(); + " ";
mydate = mydate + isdate.getseconds();
//mydate = string.fromcharcode(48 + mydate.length) + mydate;
return mydate ;
}
}
function sorttable(col, tabletosort)
{
var icurcell = col + tabletosort.cols;
var totalrows = tabletosort.rows.length;
var bsort = 0;
var colarray = new array();
var oldindex = new array();
var indexarray = new array();
var barray = new array();
var newrow;
var newcell;
var i;
var c;
var j;
// ** populate the array colarray with contents of the column selected
for (i=1; i < tabletosort.rows.length; i++)
{
colarray[i - 1] = setdatatype(tabletosort.cells(icurcell).innertext);
icurcell = icurcell + tabletosort.cols;
}
// ** copy array for comparison after sort
for (i=0; i < colarray.length; i++)
{
barray[i] = colarray[i];
}
// ** sort the column items
//alert ( colarray );
colarray.sort();
//alert ( colarray );
for (i=0; i < colarray.length; i++)
{ // loop through the new sorted array
indexarray[i] = (i+1);
for(j=0; j < barray.length; j++)
{ // loop through the old array
if (colarray[i] == barray[j])
{ // when the item in the old and new match, place the
// current row number in the proper position in the
// new order array so rows can be moved ....
// make sure current row number is not already in the
// new order array
for (c=0; c<i; c++)
{
if ( oldindex[c] == (j+1) )
{
bsort = 1;
}
}
if (bsort == 0)
{
oldindex[i] = (j+1);
}
bsort = 0;
}
}
}
// ** sorting complete, add new rows to base of table ....
for (i=0; i<oldindex.length; i++)
{
newrow = tabletosort.insertrow();
for (c=0; c<tabletosort.cols; c++)
{
newcell = newrow.insertcell();
newcell.innerhtml = tabletosort.rows(oldindex[i]).cells(c).innerhtml;
}
}
//move new rows to top of table ....
for (i=1; i<totalrows; i++)
{
tabletosort.moverow((tabletosort.rows.length -1),1);
}
//delete the old rows from the bottom of the table ....
for (i=1; i<totalrows; i++)
{
tabletosort.deleterow();
}
}
// end -->
</script>
</head>
<!-- step two: copy this code into the body of your html document -->
<body>
<p><center>
<font face="arial, helvetica" size"-2">free javascripts provided<br>
by <a href="http://javascriptsource.com">the javascript source</a></font>
</center><p>