If JavaScript is unavailable or the module fails to load, the captioned tables remain readable without sort buttons. On narrow screens, focus a named table region and use the arrow keys to scroll horizontally. Tab moves between sort buttons; Enter or Space changes the sort direction.
Sort a captioned table
Wrap a table in <table-sortable>. Without
JavaScript, it remains a readable table in its authored
order. After enhancement, each header becomes a button: the
first activation sorts ascending, and the next sorts
descending. Sorting Age compares numbers, not digits as
text.
Employee directory
Use one header row in <thead> and
matching data cells in <tbody>,
without spanning cells. The component replaces each
header’s contents with its trimmed plain text in a
button. Don’t put links, existing buttons, or markup you
need to preserve in those headers.
<script type="module" src="../define.js"></script>
<table-sortable
tabindex="0" role="region" aria-label="Employee table">
<table>
<caption>Employee directory</caption>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
<th>Occupation</th>
</tr>
</thead>
<tbody>
<tr>
<td>Charlie Brown</td>
<td>35</td>
<td>New York</td>
<td>Designer</td>
</tr>
<tr>
<td>Alice Cooper</td>
<td>28</td>
<td>Boston</td>
<td>Developer</td>
</tr>
<tr>
<td>Bob Dylan</td>
<td>42</td>
<td>Chicago</td>
<td>Manager</td>
</tr>
<tr>
<td>Diana Prince</td>
<td>31</td>
<td>Seattle</td>
<td>Engineer</td>
</tr>
<tr>
<td>Eve Anderson</td>
<td>26</td>
<td>Portland</td>
<td>Designer</td>
</tr>
</tbody>
</table>
</table-sortable>
| Name | Age | City | Occupation |
|---|---|---|---|
| Charlie Brown | 35 | New York | Designer |
| Alice Cooper | 28 | Boston | Developer |
| Bob Dylan | 42 | Chicago | Manager |
| Diana Prince | 31 | Seattle | Engineer |
| Eve Anderson | 26 | Portland | Designer |
The caption names the table; the named, focusable region
and horizontal scrolling are authored here, not added by
the component. On narrow screens, focus the region to
scroll, then Tab to a header button. Sorting keeps that
button focused and exposes its state through
aria-sort and a description, without a
second live announcement.
Custom Sort Keys
Use the data-sort-value attribute to define
custom sort values that differ from the displayed content.
Product Pricing with Custom Keys
<tr>
<td data-sort-value="WIDGET-B">Widget B (Premium)</td>
<td>WDG-002</td>
<td data-sort-value="50">$50.00</td>
<td data-sort-value="15">Low (15)</td>
</tr>
| Product | SKU | Price | Stock |
|---|---|---|---|
| Widget B (Premium) | WDG-002 | $50.00 | Low (15) |
| Widget A (Basic) | WDG-001 | $30.00 | High (100) |
| Widget C (Deluxe) | WDG-003 | $100.00 | Very Low (5) |
| Gadget A (Standard) | GDG-001 | $75.00 | Medium (50) |
data-sort-value to sort numerically rather
than alphabetically by the formatted text. An explicit
data-sort-value wins over an inline key or
the cell’s visible text. Keep key types and text-key
casing consistent within a column.
Inline Sort Keys
Wrap the visible text fragment you want to prioritize for
sorting with [data-sort-as].
Sorting by Last Name
<td>
John <span data-sort-as>Smith</span>
</td>
| Full Name | Department | |
|---|---|---|
| John Smith | john.smith@example.com | Engineering |
| Emily Anderson | emily.anderson@example.com | Marketing |
| Wei Zhang | wei.zhang@example.com | Design |
| Sarah Brown | sarah.brown@example.com | Sales |
Grouped Tables
Add the same data-table-sort-group value to
every <tr> in a group to keep those rows
together while sorting. Alternatively, put the attribute on
a <tbody> to apply that value to all of
its rows.
Department-Grouped Employee List
This excerpt gives each employee a department key. The visible department names keep that relationship clear before and after sorting.
<tbody>
<tr data-table-sort-group="engineering">
<td>Charlie Brown <span class="grouped-indicator">(Engineering)</span></td>
<td>85</td>
<td>Active</td>
</tr>
<tr data-table-sort-group="engineering">
<td>Frank Miller <span class="grouped-indicator">(Engineering)</span></td>
<td>88</td>
<td>On Leave</td>
</tr>
<tr data-table-sort-group="design">
<td>Alice Cooper <span class="grouped-indicator">(Design)</span></td>
<td>92</td>
<td>Active</td>
</tr>
</tbody>
| Name | Score | Status |
|---|---|---|
| Charlie Brown (Engineering) | 85 | Active |
| Frank Miller (Engineering) | 88 | On Leave |
| Eve Adams (Engineering) | 76 | Inactive |
| Alice Cooper (Design) | 92 | Active |
| Grace Hopper (Design) | 98 | Active |
| Bob Dylan (Sales) | 78 | Active |
| Henry Ford (Sales) | 84 | Active |
| Diana Prince (Marketing) | 95 | Active |
data-table-sort-group to each
<tr> when, as in this example,
several departments share one
<tbody>. Alternatively, apply it to a
<tbody> to give all of that body's
rows the same group. A row’s value overrides the body’s.
Sort Score ascending: Engineering comes first because
Eve has the lowest score, and its other employees stay
beside her in score order. Descending puts Design first
because Grace has the highest score. Group keys don’t
set the department order.
Department names and thicker separators identify groups without relying on the supplementary purple and green edge accents. Neutral stripes support reading, and the sorted column pairs its text and fill colors. See the grouping reference for precedence and body-attribute caveats.
Change sort descriptions
Use label-sortable to describe unsorted column
buttons. Use label-ascending and
label-descending to describe the active column
and the next available action. Set these attributes before
the component initializes. These labels can match your
application’s vocabulary as well as another language;
changing lang alone doesn’t translate them.
French Localization
<table-sortable
lang="fr" tabindex="0" role="region" aria-label="Tableau du personnel"
label-sortable="Cliquer pour trier par ordre croissant"
label-ascending="trié croissant. Cliquer pour trier décroissant"
label-descending="trié décroissant. Cliquer pour trier croissant">
<table>
<caption>Personnel</caption>
<thead>
<tr>
<th>Nom</th>
<th>Âge</th>
<th>Ville</th>
</tr>
</thead>
<tbody>
<tr><td>Marie</td><td>28</td><td>Paris</td></tr>
<tr><td>Pierre</td><td>35</td><td>Lyon</td></tr>
<tr><td>Sophie</td><td>42</td><td>Marseille</td></tr>
</tbody>
</table>
</table-sortable>
| Nom | Âge | Ville |
|---|---|---|
| Marie | 28 | Paris |
| Pierre | 35 | Lyon |
| Sophie | 42 | Marseille |
aria-sort.
Event Handling
Listen to the table-sortable:sort event to
react to sorting changes.
Sort Event Listener
const eventDemo = document.getElementById('event-demo');
const eventMessages = document.getElementById('event-messages');
let eventCount = 0;
eventDemo.addEventListener('table-sortable:sort', (event) => {
eventCount++;
const { column, direction, header } = event.detail;
const columnName = header.querySelector('button').textContent.trim();
const message = document.createElement('div');
message.textContent = `[${eventCount}] Sorted by "${columnName}" (column ${column}) in ${direction}ending order`;
if (eventMessages.querySelector('em')) {
eventMessages.replaceChildren();
}
eventMessages.prepend(message);
while (eventMessages.children.length > 5) {
eventMessages.lastChild.remove();
}
});
| Name | Value |
|---|---|
| Item C | 30 |
| Item A | 10 |
| Item B | 20 |
This listener shows the latest five sorts in
#event-messages. It reads the button’s
name, not its hidden description, and writes plain text.
The result isn’t a live region: the header’s state and
description already communicate sorting.
Style the sort indicators
The basic table uses text arrows. To choose other
characters, set
--table-sortable-indicator-asc and
--table-sortable-indicator-desc to quoted CSS
strings, such as '▲' and '▼'. See
the
styling reference
for legacy hooks and sorted-cell colors.
Use outlined and filled SVG carets
Add svg-sort-indicators with the rules in
the linked demo stylesheet.
The active direction is filled; the other caret stays
outlined. Both follow the button’s foreground color.
Browsers without mask support retain text arrows, and
forced colors use outlined and filled text carets.
<link rel="stylesheet" href="./styles.css">
<table-sortable class="svg-sort-indicators"
tabindex="0" role="region" aria-label="Employee age table">
<table>
<caption>Employee ages</caption>
<thead>
<tr><th>Name</th><th>Age</th></tr>
</thead>
<tbody>
<tr><td>Charlie Brown</td><td>35</td></tr>
<tr><td>Alice Cooper</td><td>28</td></tr>
</tbody>
</table>
</table-sortable>
| Name | Age |
|---|---|
| Charlie Brown | 35 |
| Alice Cooper | 28 |
API Reference
For complete documentation of attributes, events, and browser support, see the README.