Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ The solution is short, yet may look a bit tricky, so here I provide it with exte

```js
let sortedRows = Array.from(table.tBodies[0].rows) // 1
.sort((rowA, rowB) => rowA.cells[0].innerHTML.localeCompare(rowB.cells[0].innerHTML));
.sort((rowA, rowB) => rowA.cells[0].textContent.trim().localeCompare(rowB.cells[0].textContent.trim())); // 2

table.tBodies[0].append(...sortedRows); // (3)
table.tBodies[0].append(...sortedRows); // 3
```

The step-by-step algorthm:
Expand All @@ -15,4 +15,4 @@ The step-by-step algorthm:

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.
Tables always have an implicit `<tbody>` element, so we need to get it and insert into it: a simple `table.append(...)` will fail.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<script>
let sortedRows = Array.from(table.tBodies[0].rows)
.sort((rowA, rowB) => rowA.cells[0].innerHTML.localeCompare(rowB.cells[0].innerHTML));
.sort((rowA, rowB) => rowA.cells[0].textContent.trim().localeCompare(rowB.cells[0].textContent.trim()));

table.tBodies[0].append(...sortedRows);
</script>