Skip to content

FlashcardNot attempted

Sortable table: comparator and markup

Recall it before you reveal

What makes a sortable data table correct, both the sort itself and the header markup?

Space to reveal
Show answer & explanation
  • Sort, then paginate: sort the whole dataset (or send the sort to the server and restart at page 1), never just the loaded rows.
  • Compare by type: numbers with a - b, dates by timestamp, strings with Intl.Collator (case- and accent-aware); empty values last in both directions.
  • Stable: Array.prototype.sort is stable since ES2019; for desc flip the sign rather than reversing, and copy first ([...rows] or toSorted).
  • Toggle: same column flips asc/desc, a new column starts asc.
  • Markup: <th scope="col"> containing a <button type="button">; aria-sort on the sorted header only; arrow glyph aria-hidden; a <caption> naming the table and current sort.