APP-36Recommended
Make sortable columns buttons
Put a button inside each sortable column header, and set aria-sort on the header of the sorted column only.
How to do it
Start from table headers. The button's name is the column name. Set aria-sort="ascending" or "descending" on the sorted th and remove it from the others. Show the direction with an icon as well as the attribute. Announce the new order in a status region, such as "Sorted by amount, highest first".
Examples
HTML example
<th scope="col" aria-sort="descending">
<button type="button" data-sort="amount">Amount <svg aria-hidden="true" class="sort-icon">…</svg></button>
</th>
<th scope="col"><button type="button" data-sort="customer">Customer</button></th>
TypeScript example
export function markSorted(table: HTMLTableElement, column: string, direction: 'ascending' | 'descending') {
for (const header of table.querySelectorAll('th')) header.removeAttribute('aria-sort');
table.querySelector(`button[data-sort="${column}"]`)?.closest('th')?.setAttribute('aria-sort', direction);
}
Python example
from playwright.sync_api import expect
def test_sort_by_keyboard(page):
page.goto("https://staging.example.com/orders")
page.get_by_role("button", name="Amount").focus()
page.keyboard.press("Enter")
expect(page.locator("th[aria-sort]")).to_have_count(1)
expect(page.get_by_role("columnheader", name="Amount")).to_have_attribute("aria-sort", "ascending")
How to check
A browser test tabs to a header button and presses Enter. That header has aria-sort, no other header has it, and the status region names the order.
Sources
Related rules
- AXS-22 Give tables headers in Accessibility
- AXS-30 Announce changes without moving focus in Accessibility
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/app-ui/lists-and-tables/sortable-headers">Agent Wranglers web app interface playbook</a>. In code you write, add a comment next to it with the same link.