/**
*表格排序
*t:表格体.例:mytable.tbodies[0]
*irowend:第几行停止排序.例:mytable.tbodies[0].rows.length-1
*freverse:升序,降序.例:true(升)false(降)
*icolumn:第几列需要排序.例 4
*/
function insertionsort(t, irowend, freverse, icolumn)
{
var irowinsertrow, irowwalkrow, current, insert;
for ( irowinsert = 0 + 1 ; irowinsert <= irowend ; irowinsert++ )
{
if (icolumn)
{
if( typeof(t.children[irowinsert].children[icolumn]) != "undefined")
textrowinsert = t.children[irowinsert].children[icolumn].innertext;
else
textrowinsert = "";
}
else
{
textrowinsert = t.children[irowinsert].innertext;
}
for ( irowwalk = 0; irowwalk <= irowinsert ; irowwalk++ )
{
if (icolumn)
{
if(typeof(t.children[irowwalk].children[icolumn]) != "undefined")
textrowcurrent = t.children[irowwalk].children[icolumn].innertext;
else
textrowcurrent = "";
}
else
{
textrowcurrent = t.children[irowwalk].innertext;
}
//
// we save our values so we can manipulate the numbers for
// comparison
//
current = textrowcurrent;
insert = textrowinsert;
// if the value is not a number, we sort normally, else we evaluate
// the value to get a numeric representation
//
if ( !isnan(current) || !isnan(insert))
{
current= eval(current);
insert= eval(insert);
}
else
{
current=current.tolowercase();
insert= insert.tolowercase();
}
if ( ( (!freverse && insert < current)
|| ( freverse && insert > current) )
&& (irowinsert != irowwalk) )
{
erowinsert = t.children[irowinsert];
erowwalk = t.children[irowwalk];
t.insertbefore(erowinsert, erowwalk);
irowwalk = irowinsert; // done
}
}
}
}
参考:有3个例子.各个不赖.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndude/html/dude07232001.asp
