Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several problems with the example you gave. Number one problem is that you are selecting the columns using jquery inside the loops. This is a major performance penalty. If you have any control over the html code I would suggest you use normal DOM methods to get the desired colum you want to sort on. Note that sometimes when you might expect a table cell node, you may get a text node. I'll get back to that later. A for loop is faster so you may consider using that instead of $.each, but I suggest you benchmark it.</p> <p>I took your example and created a table with 1000 rows. It took about 750ms on my machine to sort it. I did some optimizations (see code below) and managed to get it down to 200ms. The sorting itself took around 20ms (not bad).</p> <pre><code>var sb = []; sb.push("&lt;table border='1'&gt;"); var x; for (var i = 0; i &lt; 1000; i++) { x = Math.floor(Math.random() * 1000); sb.push("&lt;tr&gt;&lt;td&gt;data"); sb.push(x); sb.push("&lt;/td&gt;&lt;/tr&gt;"); } sb.push("&lt;/table&gt;"); document.write(sb.join("")); $table = $("table"); var rows = $table.find('tbody &gt; tr').get(); var columnIndex = 0; var t = new Date(); $.each(rows, function(index, row) { row.sortKey = $(row.childNodes[columnIndex]).text(); }); alert("Sort key: " + (new Date() - t) + "ms"); t = new Date(); rows.sort(function(a, b) { return a.sortKey.localeCompare(b.sortKey); }); alert("Sort: " + (new Date() - t) + "ms"); t = new Date(); var tbody = $table.children('tbody').get(0); $.each(rows, function(index, row) { tbody.appendChild(row); delete row.sortKey; }) alert("Table: " + (new Date() - t) + "ms"); </code></pre> <p>When you write for speed you want each iteration to be as quick as possible, so don't do stuff in the loops that you can do outside of them. For example, moving $table.children('tbody').get(0); outside of the last loop sped things up enormously.</p> <p>As for using DOM methods to access a column, what you need is the column index, so you could iterate over the th columns until you find the correct one (provided the html formatting is identical for th tags and td tags). You can then use that index to get the correct row child node.</p> <p>Also, if the table is static and the users are liable to do more sorting on it, you should cache the rows and not delete the sortKey property. Then you save about 30% sort time. There is also the matter of the table contents. If the content is text, this sorting method is fine. If it contains numbers etc. then you need to consider that, since I am using localeCompare which is a method of the String kind.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload