function sort (tableId, Sortcolumn,nodetype) {
var table = document.getElementById ("thetable");
var tablebody = table.tbodies[0];
var tablerows = tablebody.rows;
var rowarray = new Array ();
for (var i = 0; i < tablerows.length; i++) {
Rowarray[i] = tablerows[i];
}
if (Table.sortcolumn = = SortColumn) {
Rowarray.reverse ();
} else {
Rowarray.sort (Generatecomparetr (SortColumn, NodeType));
}
var tbodyfragment = document.createdocumentfragment ();
for (var i = 0; i < rowarray.length; i++) {
Tbodyfragment.appendchild (Rowarray[i]);
}
Tablebody.appendchild (tbodyfragment);
Table.sortcolumn = SortColumn;
}
function Generatecomparetr (SortColumn, NodeType) {
return function Comparetr (Trleft, trright) {
var leftvalue = convert (Trleft.cells[sortcolumn].firstchild.nodevalue, nodeType);
var rightvalue = convert (Trright.cells[sortcolumn].firstchild.nodevalue, nodeType);
if (Leftvalue < Rightvalue) {
return-1;
} else {
if (Leftvalue > Rightvalue) {
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 ();
}
}
Then it is used:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://"
+ request.getservername () + ":" + request.getserverport ()
+ path + "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base <%=basePath%> ">
<title>my JSP ' tabel.jsp ' starting page</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<!--
<link rel= "stylesheet" type= "Text/css" >
-
<script type= "Text/javascript" src= "Userjs/tablesort.js" ></script>
<body>
<table id= "thetable" align= "center" border= "1" >
<thead>
<tr>
<td>
Title 1
</td>
<TD onclick= "Sort (thetable,1, ' int ')" >
Title 2
</td>
<TD onclick= "Sort (thetable,2, ' int ')" >
Title 3
</td>
<TD onclick= "Sort (thetable,3, ' int ')" >
Title 4
</td>
<TD onclick= "Sort (thetable,4, ' int ')" >
Title 5
</td>
<TD onclick= "Sort (thetable,5, ' int ')" >
Title 6
</td>
<TD onclick= "Sort (thetable,6, ' int ')" >
Title 7
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
<td>
4
</td>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
</tr>
<tr>
<td>
2
</td>
<td>
3
</td>
<td>
4
</td>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
</tr>
<tr>
<td>
3
</td>
<td>
4
</td>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
<td>
2
</td>
</tr>
<tr>
<td>
4
</td>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
</tr>
<tr>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
<td>
4
</td>
</tr>
<tr>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
<td>
4
</td>
<td>
5
</td>
</tr>
</tbody>
</table>
</body>
Jsp+js Implementing sortable Tables