اÛÙ Ù
ØØªÙا تÙÙØ§ در اÛ٠زباÙâÙØ§ Ù
ÙØ¬Ùد است: English, Español, Français, Italiano, æ¥æ¬èª, íêµì´, Ð ÑÑÑкий, Türkçe, УкÑаÑнÑÑка, OÊ»zbek, ç®ä½ä¸æ. ÙØ·Ùا٠ب٠Ù
ا
Sort the table
اÙÙ
ÛØª: 5
Thereâs a table:
There may be more rows in it.
Write the code to sort it by the "name" column.
The solution is short, yet may look a bit tricky, so here I provide it with extensive comments:
let sortedRows = Array.from(table.tBodies[0].rows) // 1
.sort((rowA, rowB) => rowA.cells[0].innerHTML.localeCompare(rowB.cells[0].innerHTML));
table.tBodies[0].append(...sortedRows); // (3)
The step-by-step algorthm:
- Get all
<tr>, from<tbody>. - Then sort them comparing by the content of the first
<td>(the name field). - Now insert nodes in the right order by
.append(...sortedRows).
We donât have to remove row elements, just âre-insertâ, they leave the old place automatically.
P.S. In our case, thereâs an explicit <tbody> in the table, but even if HTML table doesnât have <tbody>, the DOM structure always has it.