Skip to content

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

Spring 10k results
RunnerAge group
Priya NairHarbour Harriers36:4830 to 39
Amira HaddadHarbour Harriers38:1230 to 39
Lena OrtizEastside Runners39:2720 to 29
Tom BrandtEastside Runners41:0540 to 49
Sam WhitlockFell Foxes42:5140 to 49
Jonas BergFell Foxes44:3050 to 59

The page opens sorted by Finish time, fastest first, with 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

Spring 10k results
RunnerClubFinish timeAge group
Priya NairHarbour Harriers36:4830 to 39
Amira HaddadHarbour Harriers38:1230 to 39
Lena OrtizEastside Runners39:2720 to 29
Tom BrandtEastside Runners41:0540 to 49
Sam WhitlockFell Foxes42:5140 to 49
Jonas BergFell Foxes44:3050 to 59

Click Club and the rows sort. Tab through the page: no heading can be reached, because the click handler sits on the cell and there is no button inside it. A keyboard user cannot sort at all.

Don't: The arrow alone shows the order

Spring 10k results
RunnerAge group
Priya NairHarbour Harriers36:4830 to 39
Amira HaddadHarbour Harriers38:1230 to 39
Lena OrtizEastside Runners39:2720 to 29
Tom BrandtEastside Runners41:0540 to 49
Sam WhitlockFell Foxes42:5140 to 49
Jonas BergFell Foxes44:3050 to 59

The buttons work from the keyboard, but no heading has 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

Spring 10k results
RunnerAge group
Priya NairHarbour Harriers36:4830 to 39
Amira HaddadHarbour Harriers38:1230 to 39
Lena OrtizEastside Runners39:2720 to 29
Tom BrandtEastside Runners41:0540 to 49
Sam WhitlockFell Foxes42:5140 to 49
Jonas BergFell Foxes44:3050 to 59

The order is in 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 TableHead cells holds a shadcn Button variant="ghost" with the heading as its text and an icon after it. Runner and Age group hold plain text. Using a button rather 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-sort on the TableHead from header.column.getIsSorted(): "asc" gives aria-sort="ascending", "desc" gives "descending", and false gives no attribute. Put onClick={header.column.getToggleSortingHandler()} on the button, not on the cell. Our own advice: use aria-sort rather than text in the button, so the button's name stays "Finish time" whatever the order.
  • Set aria-sort on 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 ArrowUp for ascending, ArrowDown for descending and ArrowUpDown for a sortable column that is not sorted. Give the icon aria-hidden="true", since aria-sort already carries the order.
  • If a third press clears the sort, remove aria-sort and 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 key on 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 onClick on the TableHead or on a div inside it, so the sort works only with a pointer.
  • Give the th itself tabIndex={0} and role="button". The cell stops being a header, and TBL-001 is broken.
  • Show the same ArrowUpDown icon on Finish time whether it is sorted or not, with no aria-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 while aria-sort already says how the column is sorted. Keep the name to the visible heading.
  • Put aria-sort on 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

Stances on TBL-002
SourceRoleStancePage
WCAG 2.2LawRequires4.1.2 Name, Role, Value
WCAG 2.2LawRequires2.1.1 Keyboard
WCAG 2.2LawRequires1.3.1 Info and Relationships
WCAG 2.2LawSupports1.1.1 Non-text Content
Material DesignJudge No position
Apple Human Interface GuidelinesJudge SupportsLists and tables
Carbon Design SystemJudge SupportsData table: usage
Data table: accessibility
GOV.UK Design SystemExpert witness No position
Nielsen Norman GroupExpert 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