-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-html-table.html
387 lines (318 loc) · 11.4 KB
/
sort-html-table.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<!DOCTYPE html>
<html>
<head>
<title>Table Sort Algo </title>
<style>
table {
border-spacing: 0;
width: 80%;
border: 1px solid #dddd;
}
tr,
th,
td {
text-align: left;
padding: 15px;
border: 1px solid black;
}
button {
padding: 5px;
color: #3e3e3e;
background: rgba(230, 183, 183, 0.867);
}
/*tr:nth-child(even) {
background-color: #008000
} */
/* tr:nth-child(n+2){
background-color: red;
} */
.tableHeaderHorizontalStyle {
display: flex;
}
.tableHeaderHorizontalStyle>tr {
display: flex;
flex-direction: column;
}
</style>
</head>
<body>
<center>
<table id="table1">
<tr>
<th>Country</th>
<th>Capital</th>
<th>Initial Index</th>
<th>Random Num</th>
</tr>
<tr>
<td>United states of America</td>
<td>Washington DC</td>
<td>1</td>
<td>71</td>
</tr>
<tr>
<td>India</td>
<td>New Delhi</td>
<td>2</td>
<td>32</td>
</tr>
<tr>
<td>Australia</td>
<td>Canberra</td>
<td>3</td>
<td>63</td>
</tr>
<tr>
<td>Germany</td>
<td>Berlin</td>
<td>4</td>
<td>44</td>
</tr>
<tr>
<td>China</td>
<td>Beijing</td>
<td>5</td>
<td>65</td>
</tr>
<tr>
<td>Nepal</td>
<td>Kathmandu</td>
<td>6</td>
<td>25</td>
</tr>
<tr>
<td>Myanmar </td>
<td>Nay Pyi Taw</td>
<td>7</td>
<td>100</td>
</tr>
<tr>
<td>Qatar </td>
<td>Doha</td>
<td>8</td>
<td>108</td>
</tr>
</table>
<p>
<button onclick="sortTable1()">Sort1</button>
<button onclick="sortTable2()">Sort2</button>
</p>
<table id="table2">
<tbody class="tableHeadStyle">
<th></th>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Four</th>
<th>Five</th>
</tbody>
<tbody class="tableBodyStyle">
<tr>
<th>Country</th>
<td>France</td>
<td>Spain</td>
<td>Lebanon</td>
<td>Argentina</td>
<td>USA</td>
</tr>
<tr class="sectionTr">
<td> Hearder1 </td>
</tr>
<tr>
<th>Date</th>
<td>2001-01-01</td>
<td>2005-05-05</td>
<td>2002-02-02</td>
<td>2005-04-04</td>
<td>2004-04-04</td>
</tr>
<tr>
<th>Size</th>
<td>25</td>
<td>0</td>
<td>-17</td>
<td>100</td>
<td>-6</td>
</tr>
</tbody>
</table>
<hr>
<hr>
<!-- sort is working for this x-axis direction also -->
<!-- <table id="table3" style="display: flex;">
<tbody class="tableHeadStyle tableHeaderHorizontalStyle">
<th></th>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Four</th>
<th>Five</th>
</tbody>
<tbody class="tableBodyStyle tableHeaderHorizontalStyle">
<tr>
<th>Country</th>
<td>France</td>
<td>Spain</td>
<td>Lebanon</td>
<td>Argentina</td>
<td>USA</td>
</tr>
<tr class="sectionTr">
<td> Hearder1 </td>
</tr>
<tr>
<th>Date</th>
<td>2001-01-01</td>
<td>2005-05-05</td>
<td>2002-02-02</td>
<td>2005-04-04</td>
<td>2004-04-04</td>
</tr>
<tr>
<th>Size</th>
<td>25</td>
<td>0</td>
<td>-17</td>
<td>100</td>
<td>-6</td>
</tr>
</tbody>
</table> -->
</center>
<script>
// table sort2
// sort based on the
/*
1) first sort the given rowIndex tds values ..
and then
2) based on the new indices of the tds ..
3) just put the tds values of other trs at the respective place
as in the given rowIndex tr
*/
let isAsc = true;
function sortTable2() {
let sortRowIndex = 0;
isAsc = !isAsc;
var i, x, y;
let table = document.getElementById("table2");
// thead rows
let tHeadRows = table.querySelectorAll('.tableHeadStyle tr');
console.log('tHeadRows : ', tHeadRows)
// tbody tBodyRows
// we don't have to include the sectionTr..
// let tBodyRows = table.querySelectorAll('.tableBodyStyle tr:not(.sectionTr)');
let tBodyRows = table.querySelectorAll('.tableBodyStyle tr');
console.log('tBodyRows : ', tBodyRows)
console.log('tBodyRows length : ', tBodyRows.length)
let sortRow = tBodyRows[sortRowIndex];
console.log('sortRow : ', sortRow);
let tds = sortRow.getElementsByTagName('td');
let tdsLen = tds.length;
// sort the tds values
for (let j = 0; j < tdsLen - 1; j++) {
let min = j;
for (let k = j + 1; k < tdsLen; k++) {
if ((tds[k] && tds[min]) && (tds[k].innerHTML.toLowerCase() < tds[min].innerHTML.toLowerCase())) {
min = k;
}
}
// swap the values
if (min != j) {
let temp1 = tds[j].innerHTML;
tds[j].innerHTML = tds[min].innerHTML;
tds[min].innerHTML = temp1;
// swap/sort td values in other tBodyRows
for (let z = 0; z < tBodyRows.length; z++) {
if (z != sortRowIndex) {
let tempTDs = tBodyRows[z].getElementsByTagName('td');
if(tempTDs && tempTDs.length > 0 && tempTDs[j] && tempTDs[min]){
let temp12 = tempTDs[j].innerHTML;
tempTDs[j].innerHTML = tempTDs[min].innerHTML;
tempTDs[min].innerHTML = temp12;
}
}
}
// swap/sort td/th values in other tHeadRows
for (let z = 0; z < tHeadRows.length; z++) {
let tempTDs = tHeadRows[z].getElementsByTagName('th');
if(tempTDs && tempTDs.length > 0 && tempTDs[j] && tempTDs[min]){
let temp12 = tempTDs[j + 1].innerHTML;
tempTDs[j + 1].innerHTML = tempTDs[min + 1].innerHTML;
tempTDs[min + 1].innerHTML = temp12;
}
}
}
}
}
function sortTable1() {
var table, i, x, y;
table = document.getElementById("table1");
var switching = true;
// Run loop until no switching is needed
while (switching) {
switching = false;
var rows = table.rows;
console.log('rows : ', rows)
// Loop to go through all rows
for (i = 1; i < (rows.length - 1); i++) {
var Switch = false;
// Fetch 2 elements that need to be compared
x = rows[i].getElementsByTagName("TD")[3];
y = rows[i + 1].getElementsByTagName("TD")[3];
console.log(typeof x.innerHTML)
console.log(typeof y.innerHTML)
// Check if 2 rows need to be switched
if (x.innerHTML > y.innerHTML) {
// If yes, mark Switch as needed and break loop
Switch = true;
break;
}
}
if (Switch) {
// Function to switch rows and mark switch as completed
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
// BACKUP CODE
function sortTable3() {
let sortRowIndex = 1;
isAsc = !isAsc;
var table, i, x, y;
table = document.getElementById("table2");
var rows = table.rows;
console.log('rows : ', rows)
console.log('rows length : ', rows.length)
let sortRow = rows[sortRowIndex];
console.log('sortRow : ', sortRow);
let tds = sortRow.getElementsByTagName('td');
let tdsLen = tds.length;
// sort the tds values
for (let j = 0; j < tdsLen - 1; j++) {
let min = j;
for (let k = j + 1; k < tdsLen; k++) {
if (tds[k].innerHTML.toLowerCase() < tds[min].innerHTML.toLowerCase()) {
min = k;
}
}
// swap the values
if (min != j) {
let temp1 = tds[j].innerHTML;
tds[j].innerHTML = tds[min].innerHTML;
tds[min].innerHTML = temp1;
// swap/sort td values in other rows
for (let z = 0; z < rows.length; z++) {
if (z != sortRowIndex) {
let tempTDs = rows[z].getElementsByTagName('td');
let temp12 = tempTDs[j].innerHTML;
tempTDs[j].innerHTML = tempTDs[min].innerHTML;
tempTDs[min].innerHTML = temp12;
}
}
}
}
}
</script>
</body>
</html>