topical media & game development

talk show tell print

professional-javascript-12-TableSortExample3.htm / htm

          <title>Table Sort Example</title>
          <script type="text/javascript">
              function generateCompareTRs(iCol) {
                  return  function compareTRs(oTR1, oTR2) {
                              var sValue1 = oTR1.cells[iCol].firstChild.nodeValue;
                              var sValue2 = oTR2.cells[iCol].firstChild.nodeValue;
                              return sValue1.localeCompare(sValue2);
              function sortTable(sTableID, iCol) {
                  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[i] = colDataRows[i];
                  if (oTable.sortCol == iCol) {
                  } else {
                  var oFragment = document.createDocumentFragment();
                  for (var i=0; i < aTRs.length; i++) {
                  oTable.sortCol = iCol;
          <p>Click on the table header to sort in ascending order.</p>
          <table border="1" id="tblSort">
                      <th onclick="sortTable('tblSort', 0)" style="cursor:pointer">Last Name</th>
                      <th onclick="sortTable('tblSort', 1)" style="cursor:pointer">First Name</th>

(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.