TBL-002 Put a keyboard sort control in a sortable header, and expose its order in code
- Level
- Rule
- Verdict
- Requirement
- Area
- Tables
- Status
- proposed, version 1.0.1
Rule
When a column's header sorts the table, make the sort a button or link inside the header cell that works from the keyboard. Expose the sorted column's order in code, ascending or descending, so the direction is never shown by an arrow icon alone.
Examples
Live examples built with Justified's components. Try them with the mouse, the keyboard or a screen reader.
Do: A button in the header, the order in aria-sort
| Runner | Age group | ||
|---|---|---|---|
| Priya Nair | Harbour Harriers | 36:48 | 30 to 39 |
| Amira Haddad | Harbour Harriers | 38:12 | 30 to 39 |
| Lena Ortiz | Eastside Runners | 39:27 | 20 to 29 |
| Tom Brandt | Eastside Runners | 41:05 | 40 to 49 |
| Sam Whitlock | Fell Foxes | 42:51 | 40 to 49 |
| Jonas Berg | Fell Foxes | 44:30 | 50 to 59 |
aria-sort already on that column. Tab to Club and press Enter or Space: the rows move, focus stays on the button and the note under the table follows. Press it again to reverse the order. The arrow is decoration; the order is in the code and in the status line.Don't: The click is on the header cell
| Runner | Club | Finish time | Age group |
|---|---|---|---|
| Priya Nair | Harbour Harriers | 36:48 | 30 to 39 |
| Amira Haddad | Harbour Harriers | 38:12 | 30 to 39 |
| Lena Ortiz | Eastside Runners | 39:27 | 20 to 29 |
| Tom Brandt | Eastside Runners | 41:05 | 40 to 49 |
| Sam Whitlock | Fell Foxes | 42:51 | 40 to 49 |
| Jonas Berg | Fell Foxes | 44:30 | 50 to 59 |
Don't: The arrow alone shows the order
| Runner | Age group | ||
|---|---|---|---|
| Priya Nair | Harbour Harriers | 36:48 | 30 to 39 |
| Amira Haddad | Harbour Harriers | 38:12 | 30 to 39 |
| Lena Ortiz | Eastside Runners | 39:27 | 20 to 29 |
| Tom Brandt | Eastside Runners | 41:05 | 40 to 49 |
| Sam Whitlock | Fell Foxes | 42:51 | 40 to 49 |
| Jonas Berg | Fell Foxes | 44:30 | 50 to 59 |
aria-sort, so the note under the table shows none. Press Finish time twice: the arrow turns, and a person who cannot see it does not know whether the fastest runner is at the top or at the bottom.Don't: A button named Sort by finish time
| Runner | Age group | ||
|---|---|---|---|
| Priya Nair | Harbour Harriers | 36:48 | 30 to 39 |
| Amira Haddad | Harbour Harriers | 38:12 | 30 to 39 |
| Lena Ortiz | Eastside Runners | 39:27 | 20 to 29 |
| Tom Brandt | Eastside Runners | 41:05 | 40 to 49 |
| Sam Whitlock | Fell Foxes | 42:51 | 40 to 49 |
| Jonas Berg | Fell Foxes | 44:30 | 50 to 59 |
aria-sort, as it should be. But the button now has the name “Sort by finish time”, an instruction read out on every visit, and the same name is given to the column heading, so a screen reader says “Sort by club” before each value in the column. The name still contains the visible words, so this is our own advice and not a failure of the law: keep the name to the heading.Why
A heading that sorts when clicked is a control, even if it looks like text. A keyboard user cannot reach a header cell with a click handler, and a screen reader announces it as a column heading, not as something to press. And when an arrow is the only sign of the order, a person who cannot see it does not know whether the fastest runner is at the top or the bottom.
Three criteria make this a requirement. WCAG 2.2, 2.1.1 Keyboard asks that sorting works from the keyboard. 4.1.2 Name, Role, Value asks that the control has a role and a name in code. 1.3.1 Info and Relationships asks that what the arrow tells sighted people is in the code too. A button or link inside the th meets the first two and keeps the cell a header, which TBL-001 needs. The order can be given with aria-sort on the sorted column's th, or as text inside the control. The criteria do not choose between them. 1.1.1 Non-text Content supports the rule: an arrow that shows the order carries information, and once the order is in code, the arrow is decoration and needs no text of its own. The criteria do not ask that sorting is offered in the header at all; a product can offer it elsewhere, such as in a Sort by select.
Do
- A club's page Spring 10k results has the columns Runner, Club, Finish time and Age group. Club and Finish time can be sorted. Each of their
TableHeadcells holds a shadcnButton variant="ghost"with the heading as its text and an icon after it. Runner and Age group hold plain text. Using abuttonrather than a link is our own choice; a link that loads the sorted page, such as?sort=time, meets the rule too. - With TanStack Table, set
aria-sorton theTableHeadfromheader.column.getIsSorted():"asc"givesaria-sort="ascending","desc"gives"descending", andfalsegives no attribute. PutonClick={header.column.getToggleSortingHandler()}on the button, not on the cell. Our own advice: usearia-sortrather than text in the button, so the button's name stays "Finish time" whatever the order. - Set
aria-sorton one column at a time, and remove it from a column when another one is sorted. - The page opens sorted by Finish time, fastest first. That column already has
aria-sort="ascending"and an up arrow when the page loads. - Our own advice: change the icon with the state, such as lucide's
ArrowUpfor ascending,ArrowDownfor descending andArrowUpDownfor a sortable column that is not sorted. Give the iconaria-hidden="true", sincearia-sortalready carries the order. - If a third press clears the sort, remove
aria-sortand show the unsorted icon again. Whether the product offers that third press is its own choice. - Our own advice: keep focus on the same button while the rows re-render, so the next Enter reverses the order and the next Tab goes on from there. Keep a stable
keyon the header cells, so React does not replace the button that has focus. - Our own advice, beyond the rule: say the new order in a status message, such as "Sorted by finish time, fastest first", as FBK-004 describes.
- When a narrow layout turns the table into a list, offer the sort as a select with the visible label Sort by (FRM-001). Its chosen value then shows and exposes the order.
Don't
- Put
onClickon theTableHeador on adivinside it, so the sort works only with a pointer. - Give the
thitselftabIndex={0}androle="button". The cell stops being a header, and TBL-001 is broken. - Show the same
ArrowUpDownicon on Finish time whether it is sorted or not, with noaria-sort. - Give the button the
aria-label"Sort by finish time". This is our own advice: the name contains the visible words, so ACT-001b is met, but it adds an instruction that is read out on every visit whilearia-sortalready says how the column is sorted. Keep the name to the visible heading. - Put
aria-sorton the button instead of the header cell. - Rebuild the header row after each sort so focus drops to the start of the page.
Exceptions
- A sortable column that looks plain at rest. The rule does not ask that a column that can be sorted shows it before the person hovers or focuses it. Showing a sign at rest is a design choice. Once a column is sorted, its state shows and is in code.
- Several sort columns at once. Sorting by more than one column at once is outside this rule.
- Neighbouring rules. The header cells themselves are TBL-001. Keyboard operation and the focus indicator in general are KBD-001, and this rule is the table case with the state added. The button's role is ACT-009, its name ACT-001b, and an announcement of the new order is FBK-004.
Verdict
Requirement. Only the judges decide; the expert witnesses are shown but do not vote. How verdicts work
| Source | Role | Stance | Page |
|---|---|---|---|
| WCAG 2.2 | Law | Requires | 4.1.2 Name, Role, Value |
| WCAG 2.2 | Law | Requires | 2.1.1 Keyboard |
| WCAG 2.2 | Law | Requires | 1.3.1 Info and Relationships |
| WCAG 2.2 | Law | Supports | 1.1.1 Non-text Content |
| Material Design | Judge | No position | |
| Apple Human Interface Guidelines | Judge | Supports | Lists and tables |
| Carbon Design System | Judge | Supports | Data table: usage Data table: accessibility |
| GOV.UK Design System | Expert witness | No position | |
| Nielsen Norman Group | Expert witness | No position |
Each stance links to the page it rests on, read on the date in the source register. This page never repeats what a source says; read the source itself.
History
- Version 1.0.1, : Points to ACT-009 for the sort control's role.
- Version 1.0.0, : First version.
Last checked against the other rules for conflicts on .
More in Tables: 3 rules and 1 guideline · All rules
Justified is an independent project. It is not affiliated with, sponsored or endorsed by the organisations whose guidance it cites. Read the full statement.
Apple is a trademark of Apple Inc., registered in the U.S. and other countries and regions.