Home > Article > Web Front-end > JavaScript study notes (4) table sorting
The table sorting implemented in this article can be roughly divided into the following steps:
1. Get all the rows to be sorted, and push their references into an array
2. Write the array used when sorting according to the rows to be sorted Comparison function
3. Sort the array containing all row references 4. Write the sorted array back to the DOM in the specified order with the rows referenced by the array
If you are not familiar with using DOM to operate tables, you can refer to "Using DOM to Write Browser-Compatible Table Operations". If you are not familiar with array sorting, you can refer to "Array Sorting and Sorting in Chinese Characters" "The use of localeCompare() method", because using DOM to operate tables and array sorting is the basis of table sorting.
Let’s first take a look at the code used in our example. This article will analyze it step by step according to the steps mentioned above:
1 var asc = true;
2 var arrayTr = []; // Container that stores all row references to be sorted
3 Function Sorttable () {
4 // Obtain all the lines to be sorted. . ROWS ;
8 }
12 /If it is in ascending order
13 ’ s ’ s ’ s ’ s ’ through together to ’ s ’s ’ through ’s through using out through out out out out out out out out out Out out out of when to ward over-becoming sorting with being sorting-- asc = false;
17 } else {
18
22 Rows are rewritten back to the DOM in numerical order
24 var oFragment = document.createDocumentFragment();
25 for(var j =0; j < arrayTr.length;j++){
26 [j]);
27 oTBody. ;thead/> and
The above is the content of the table sorting in JavaScript study notes (4). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!