79Build a sortable and filterable data table
mediumAbout 35 minutesCodingreacttablessortingaccessibility
Problem
Build <DataTable columns={columns} rows={rows} /> that sorts and filters on the client.
columns is an array of { key, label, type: "string" | "number" }; rows is an array of objects.
- A search box
<input aria-label="Search">keeps only rows where any column contains the text (case-insensitive). - Each header
<th>contains a<button>with the column label. Clicking it cycles the sort: ascending → descending → none (back to the original order). The<th>hasaria-sort="ascending" | "descending" | "none". - Number columns sort numerically (
9before10); string columns uselocaleComparewith natural numbers. - When nothing matches, render a single row with
No results.
Requirements
- Sorting is stable: rows with equal values keep their original relative order.
- Never mutate the
rowsprop. - Only one column is sorted at a time; clicking another column starts it at ascending.
Try it yourself
Write your solution, run it, then submit to check it against the tests.
App.jsxReact + DOM
Step-by-step
query and sort ({ key, direction } or null). The visible rows are derived from props and state, never stored.
Unlock the full solution
What's inside
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Tests
Includes 2 code listings, 5 edge cases, 4 follow-up questions.
One-time payment by UPI, card or net banking. Launch price.