Skip to content

Commit 50395ec

Browse files
committed
minor fixes
1 parent b015213 commit 50395ec

File tree

2 files changed

+2
-14
lines changed

2 files changed

+2
-14
lines changed

2-ui/1-document/07-modifying-document/12-sort-table/solution.md

+1-8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ The solution is short, yet may look a bit tricky, so here I provide it with exte
22

33
```js
44
let sortedRows = Array.from(table.tBodies[0].rows) // 1
5-
.sort((rowA, rowB) => { // 2
6-
let valueA = rowA.cells[0].innerHTML;
7-
let valueB = rowB.cells[0].innerHTML;
8-
return valueA > valueB ? 1 :
9-
valueA == valueB ? 0 : -1;
10-
});
5+
.sort((rowA, rowB) => rowA.cells[0].innerHTML.localeCompare(rowB.cells[0].innerHTML));
116

127
table.tBodies[0].append(...sortedRows); // (3)
138
```
@@ -20,6 +15,4 @@ The step-by-step algorthm:
2015

2116
We don't have to remove row elements, just "re-insert", they leave the old place automatically.
2217

23-
Please note: our comparison function `(2)` returns `0` for equal table rows. That's handy for cases when we accidentally re-sort the table. E.g. if our users triggers such re-sorting. Returning `0` makes sure that same-named users won't get re-ordered, as opposed to a more simpler solution `valueA > valueB ? 1 : -1`.
24-
2518
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.

2-ui/1-document/07-modifying-document/12-sort-table/solution.view/index.html

+1-6
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@
2424

2525
<script>
2626
let sortedRows = Array.from(table.tBodies[0].rows)
27-
.sort((rowA, rowB) => {
28-
let valueA = rowA.cells[0].innerHTML;
29-
let valueB = rowB.cells[0].innerHTML;
30-
return valueA > valueB ? 1 :
31-
valueA == valueB ? 0 : -1;
32-
});
27+
.sort((rowA, rowB) => rowA.cells[0].innerHTML.localeCompare(rowB.cells[0].innerHTML));
3328

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

0 commit comments

Comments
 (0)