<HTML>
 
<style type="text/css">
th {background-color: #999999;
   cursor:pointer}
td {background-color: #999900;
  text-align:center}
</style><SCRIPT LANGUAGE="javaScript">
function sortCells(i) {
var column=document.all.ot1.rows(1).cells.length;
var row=document.all.ot1.rows.length;
var temp;
//定义二维数组,因为JAVASCRIPT不支持直接定义二维数组
var Ar=new Array(row-1);
for(x=0;x<row-1;x++){
 Ar[x]=new Array(column);
}
//初始化二维数组
for(x=1;x<row;x++){
 for(y=0;y<column;y++){
  Ar[x-1][y]=document.all.ot1.rows(x).cells(y).innerText;
 }
}
//数组排序--这个可以汉字排序
Ar.sort(function(a,b){return a[i].localeCompare(b[i])});
//数组排序--这个非汉字排序
/*for(x=0;x<row-1;x++){
 for(y=1;y<row-1;y++){
  temp=Ar[y-1];
  if(Ar[y-1][i]>Ar[y][i])
   {
   Ar[y-1]=Ar[y];
   Ar[y]=temp;
  }
 }
}*///输出排序好的数组
for(x=1;x<row;x++){
 for(y=0;y<column;y++){
 document.all.ot1.rows(x).cells(y).innerText=Ar[x-1][y];
 }
}
}</SCRIPT>
<BODY>
<TABLE id="ot1" align="center" width="40%" Height="40%" id=oTable border=1>
<TR>
 <TH onclick="sortCells(0)">列1</TH>
 <TH onclick="sortCells(1)">列2</TH>
 <TH onclick="sortCells(2)">列3</TH>
 <TH onclick="sortCells(3)">列4</TH>
</TR>
<TR>
 <TD>1</TD><TD>a</TD><TD>1</TD><TD>啊</TD>
</TR>
<TR>
 <TD>3</TD><TD>b</TD><TD>4</TD><TD>不</TD>
</TR>
<TR>
 <TD>5</TD><TD>c</TD><TD>4</TD><TD>才</TD>
</TR>
<TR>
 <TD>4</TD><TD>d</TD><TD>7</TD><TD>的</TD>
</TR>
<TR>
 <TD>8</TD><TD>e</TD><TD>9</TD><TD>饿</TD>
</TR>
<TR>
 <TD>2</TD><TD>f</TD><TD>3</TD><TD>发</TD>
</TR>
<TR>
 <TD>6</TD><TD>g</TD><TD>5</TD><TD>个</TD>
</TR>
<TR>
 <TD>5</TD><TD>h</TD><TD>2</TD><TD>和</TD>
</TR>
</TABLE>
</BODY>
</HTML>