// Sorttable. JS File
Function convert (svalue, sdatatype)
{
Switch (sdatatype)
{
Case "int ":
Return parseint (svalue );
Break;
Case "float ":
Return parsefloat (svalue );
Break;
Case "date ":
Return new date (date. parse (svalue ));
Break;
Default:
Return svalue. tostring ();
}
}
Function generatecomparetrs (icol, sdatatype)
{
Return function comparetrs (otr1, otr2)
{
VaR vvalue1 = convert (otr1.cells [icol]. firstchild. nodevalue, sdatatype );
VaR vvalue2 = convert (otr2.cells [icol]. firstchild. nodevalue, sdatatype );
If (vvalue1 <vvalue2)
{
Return-1;
}
Else if (vvalue1> vvalue2)
{
Return 1;
}
Else
{
Return 0;
}
}
}
Function sorttable (stableid, icol, sdatatype)
{
VaR otable = Document. getelementbyid (stableid );
VaR otbody = otable. tbodies [0];
VaR coldatarows = otbody. Rows;
VaR ATRS = new array ();
For (VAR I = 0; I <coldatarows. length; I ++)
{
ATRS. Push (coldatarows [I]);
}
If (otable. sortcol = icol)
{
ATRS. Reverse ();
}
Else
{
ATRS. Sort (generatecomparetrs (icol, sdatatype ));
}
VaR ofragment = Document. createdocumentfragment ();
For (VAR I = 0; I <ATRS. length; I ++)
{
Ofragment. appendchild (ATRS [I]);
}
Otbody. appendchild (ofragment );
Otable. sortcol = icol;
}
// The End
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>
<SCRIPT type = "text/JavaScript" src = "sorttable. js"> </SCRIPT>
</Head>
<Body>
<Table border = "1" id = "tblsort">
<Thead>
<Tr>
<TH onclick = "sorttable ('tblsort ', 0)" style = "cursor: Pointer"> last name </Th>
<TH onclick = "sorttable ('tblsort ', 1)" style = "cursor: Pointer"> first name </Th>
<TH onclick = "sorttable ('tblsort ', 2, 'date')" style = "cursor: Pointer"> birthday </Th>
<TH onclick = "sorttable ('tblsort ', 3, 'int')" style = "cursor: Pointer"> siblings </Th>
</Tr>
</Thead>
<Tbody>
<Tr>
<TD> Smith </TD>
<TD> JOHN </TD>
<TD> 7/12/1978 </TD>
<TD> 2 </TD>
</Tr>
<Tr>
<TD> sjohnson </TD>
<TD> Betty </TD>
<TD> 10/15/1977 </TD>
<TD> 4 </TD>
</Tr>
<Tr>
<TD> Henderson </TD>
<TD> Nathan </TD>
<TD> 2/25/1949 </TD>
<TD> 1 </TD>
</Tr>
<Tr>
<TD> Williams </TD>
<TD> James </TD>
<TD> 7/8/1980 </TD>
<TD> 4 </TD>
</Tr>
<Tr>
<TD> Gilliam </TD>
<TD> Michael </TD>
<TD> 7/22/1949 </TD>
<TD> 1 </TD>
</Tr>
<Tr>
<TD> Walker </TD>
<TD> Matthew </TD>
<TD> 1/14/2000 </TD>
<TD> 3 </TD>
</Tr>
</Tbody>
</Table>
</Body>
</Html>