Jump to content

Eric Dubru

Members
  • Posts

    1
  • Joined

  • Last visited

Everything posted by Eric Dubru

  1. I am also a beginner. I had the same problem. I have developped a function and it's possible to sort a table with ~ 7 000 rows in less than 1 second. It's the following function with a few comments. Eric // 'Table': table element // 'FirstRow': first row to sort (0 if no header and first row must also be sorted) // 'ColSort': order number of the column used to sort (0 for he first column) // 'VMode': mode (0: alphabetical, 1: numeric) // 'VInv': order (true: ascending, false: descending) // Other parameters are possible (ex.: lowercase/uppercase/with case, detect not numeric values,...) function NewSortTable(Table, FirstRow, ColSort, VMode, VInv) { //var T0 = GiveTime() var i, ValA, RowsDat = [], RId, Ce, Row, TabRows = Table.getElementsByTagName('TR') // Table and table rows // Read rows data (value of the cell and id of the row) for (i = FirstRow; i < TabRows.length; i++) { Row = TabRows; RId = Row.id; if (String(RId) == '') {RId = 'TmpSort' + String(i); Row.id = RId;} Ce = Row.getElementsByTagName('TD')[ColSort]; if (VMode == 0) {ValA = Ce.innerHTML; ValA = ValA.toLowerCase();} // Alphabetical sorting - Use displayed value in lowercase if (VMode == 1) {ValA = Number(Ce.getAttribute('data-nv'));} // Numbers sorting - Use actual memorized value RowsDat.push([ValA, RId]); } // Sort rows data (value and id) by value if (VMode == 0) // Alphabetical sorting { if (!VInv) {RowsDat.sort(function(a,b) {if (a[0] > b[0]) {return 1}; if (a[0] < b[0]) {return -1}; return 0})} // Normal order if (VInv) {RowsDat.sort(function(a,b) {if (a[0] < b[0]) {return 1}; if (a[0] > b[0]) {return -1}; return 0})} // Reverse order } if (VMode == 1) // Numbers sorting { if (!VInv) {RowsDat.sort(function(a,b) {return a[0] - b[0]});} // Normal order if (VInv) {RowsDat.sort(function(a,b) {return b[0] - a[0]});} // Reverse order } // Change the place of the rows for (i = 0; i < RowsDat.length; i++) { RId = RowsDat[1]; Row = document.getElementById(RId); if (RId.substring(0, 7) == 'TmpSort') {Row.removeAttribute('id');} Table.insertBefore(Row, TabRows[FirstRow]); } //alert((GiveTime() - T0) / 1000 + ' seconds') }
×
×
  • Create New...