Javascript table column sorting

Source: Internet
Author: User
Code

<Script language = "JavaScript">

// Sort the table by column and column data type
Function sorttable (tableid, icol, datatype ){
 
VaR docfrag = Document. createdocumentfragment (); // create document fragments
 
VaR sortarr = getsorttrs (tableid, icol, datatype );

For (VAR I = 0; I <sortarr. length; I ++ ){
 
Docfrag. appendchild (sortarr [I]);
 
}

VaR otable = Document. getelementbyid (tableid );

Otable. tbodies [0]. appendchild (docfrag );
 
Otable. sortcol = icol;

}

// Return the sorted rows
Function getsorttrs (tableid, icol, datatype ){
VaR trarr = new array;
VaR otable = Document. getelementbyid (tableid );
VaR rows = otable. tbodies [0]. Rows;
For (VAR I = 0; I <rows. length; I ++ ){
Trarr [I] = rows [I];
}
If (otable. sortcol = icol ){
Trarr. Reverse ();
} Else {
Trarr. Sort (comparetrs (icol, datatype ));
}
Return trarr;
}
Function comparetrs (icol, datatype ){
Return function compare (tr1, tr2 ){
VaR value1 = convert (tr1.cells [icol]. firstchild. nodevalue, datatype );
VaR value2 = convert (tr2.cells [icol]. firstchild. nodevalue, datatype );
 
If (value1 <value2 ){
Return-1;
} Else if (value1> value2 ){
Return 1;
} Else {
Return 0;
}
}
}
Function convert (value, datatype ){
Switch (datatype ){
Case "int": Return parseint (value );
Case "float": Return parsefloat (value );
Case "date": return new date (date. parse (value ));
Default: return value. tostring ();
}
}

</SCRIPT>

Code

<Table border = "1" id = "sorttable"> <thead>
<Tr background = "blue">
<TD onmouseover = "javascript: This. style. cursor = 'hand' "onclick =" sorttable ('sorttable', 0, 'string'); "> string sorting </TD>
 
<TD onmouseover = "javascript: This. style. cursor = 'hand' "onclick =" sorttable ('sorttable', 1, 'int'); "> Number sorting </TD>
 
</Tr>
</Thead>
 
<Tr> <TD> Li Kang </TD> <TD> 3 </TD> </tr>
 
<Tr> <TD> Li Jie </TD> <TD> 4 </TD> </tr>
 
<Tr> <TD> Li Kang </TD> <TD> 3 </TD> </tr>
 
<Tr> <TD> Zhu minjian </TD> <TD> 8 </TD> </tr>
 
<Tr> <TD> Zhu Jie </TD> <TD> 7 </TD> </tr>
 
<Tr> <TD> Yang leyong </TD> <TD> 5 </TD> </tr>

</Table>

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.