pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/javascript-tutorial/en.javascript.info/pull/3922/files

assets.com/assets/primer-primitives-10bf9dd67e3d70bd.css" /> feat: update sort-table solution.md en by front42 · Pull Request #3922 · javascript-tutorial/en.javascript.info · GitHub
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>
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy