var generateCompareTRs = function( iCol, sType, iDirection ) { if( ! iDirection ) { iDirection = 1; } if( sType == "a" ) { return function ( oTR1, oTR2 ) { var sValue1 = oTR1.cells[iCol].firstChild.nodeValue; var sValue2 = oTR2.cells[iCol].firstChild.nodeValue; return sValue1.localeCompare( sValue2 ) * iDirection; }; } else { return function ( oTR1, oTR2 ) { var nValue1 = parseInt( oTR1.cells[iCol].firstChild.nodeValue.replace( /\D/, "" )); var nValue2 = parseInt( oTR2.cells[iCol].firstChild.nodeValue.replace( /\D/, "" )); var iResult; if( nValue1 < nValue2 ) { iResult = -1; } else if ( nValue1 > nValue2 ) { iResult = 1; } else { iResult = 0; } iResult = iResult * iDirection; return iResult; }; } }; var sortTable = function( oColumn ) { var oRow = oColumn.parentNode; var iCellIdx = 0; var sCellIdx = oColumn.getAttribute( "fse:idx" ); if( sCellIdx ) { iCellIdx = parseInt( sCellIdx ); } else { for( var i = 0; i < oRow.cells.length; i++ ) { if( oRow.cells[i] == oColumn ) { iCellIdx = i; } } } var iDirection = oColumn.getAttribute( "fse:dir" ); if( ! iDirection ) { iDirection = -1; } var sType = oColumn.getAttribute( "fse:type" ); if( !sType ) { sType = "a"; } iDirection = iDirection * -1; oColumn.setAttribute( "fse:dir", iDirection ); var oTable = oRow.parentNode.parentNode; // 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] ); } aTRs.sort( generateCompareTRs( iCellIdx, sType, iDirection ) ); var oFragment = document.createDocumentFragment(); for( var i = 0; i < aTRs.length; i++ ) { oFragment.appendChild( aTRs[i] ); } oTBody.appendChild( oFragment ); };