Lesson 3 of 8

Sortable table

Goal

A header that sorts the table on click and announces its state

01

Explanation

A sortable table needs three things: a column header you can click, a sort that reverses direction on the second click, and the aria-sort attribute to announce the sort state.

aria-sort takes ascending, descending or none, and only one column carries the sort state at any moment.

Sort the data itself, not the DOM elements: sort the array, then redraw the table from it. Code that rearranges rows in place ends up longer and breaks at the first change.

02

Your task

Make clicking a column header sort the table by that column, make a second click reverse the direction, and update aria-sort on every click.

Checks

6 checks
  1. The table starts with three rows
  2. Sorts by score in ascending order
  3. A second click reverses the direction
  4. aria-sort announces the state
  5. Only one column carries the sort state
  6. Sorting by name works and the original array is unchanged
03

Hints

0/3
Track lessons0 of 8 done

Build from scratch, on your own, the components you'll find on almost every website, such as tabs, dialogs, tables, toasts and a carousel.

Advanced level, 8 lessons, 2 hours in total.

index.html
Preview
index.html100%
–/6

Press “Run and check” to grade your code.

6 checks listed under “Your task”.

Track lessons0 of 8 done

Build from scratch, on your own, the components you'll find on almost every website, such as tabs, dialogs, tables, toasts and a carousel.

Advanced level, 8 lessons, 2 hours in total.