Data table
Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.
Build a sortable, paginated data table. users.js holds 48 users, and App.js renders them all in one long, unsorted table.
Requirements
Sorting
- Clicking a column header sorts the table by that column, ascending.
- Clicking the same header again flips it to descending, and clicking again flips it back.
- Clicking a different header sorts by that column, ascending.
- Numbers sort as numbers (
9before10) and text sorts alphabetically. - The sorted header shows a direction indicator (
▲/▼). Unsorted columns can show↕.
Pagination
- Show 10 rows per page, with Previous and Next buttons and a
Page 2 of 5label. - Previous is disabled on the first page and Next on the last.
- A Rows per page select offers 5, 10 and 20. Changing it, or changing the sort, goes back to page 1.
Accessibility
- Each header's sort control is a
<button>inside the<th>. - The sorted column's
<th>hasaria-sort="ascending"or"descending".
Note
The preview updates as you type. There are no automated tests for this challenge yet: sort by each column in both directions and page through the results, then compare with the reference solution.
Follow-ups interviewers ask
- Add a search box that filters rows. Where does filtering go relative to sorting and paging?
- Move sorting and paging to the server: what changes in the component's state and data flow?
- Keep the sort and page in the URL so the view can be shared and survives a refresh.
- The table has 50,000 rows: what do you do? (Hint: virtualization.)
The approach
The state is only what the user chose: the sort ({ key, direction }), the page size and the current page. Everything shown is derived from it in a pipeline:
Storing the sorted array or the current page's rows in state would mean keeping them in sync with the sort and page by hand. Deriving them can't go wrong.
Sorting
[...users].sort(...)sorts a copy.Array.prototype.sortmutates in place, and mutating imported data (or props) is a classic bug: the original order is lost, and React may not notice the change.- The comparator subtracts numbers and uses
localeComparefor strings. A plaina < bcomparison on mixed types, or sorting numbers as strings, puts10before9. - Multiplying by
sign(1or-1) handles both directions with one comparator. sortis stable in modern JavaScript, so rows with equal values keep their previous relative order.useMemokeeps the sorted array between renders, so changing the page doesn't sort again. With 48 rows this is not needed for speed, but it is the right habit when the data grows.
Pagination
slice((page - 1) * pageSize, page * pageSize) takes the current page. Math.max(1, ...) keeps pageCount at least 1, so an empty table shows Page 1 of 1, not Page 1 of 0.
Changing the sort or the page size resets the page to 1. Otherwise a user on page 5 who switches to 20 rows per page would land on a page that no longer exists.
Accessibility
The sort control is a real <button> inside the <th>. A clickable <th> isn't focusable and isn't announced as a control. aria-sort on the <th> tells screen readers which column is sorted and in which direction, and the arrows are aria-hidden because aria-sort already says it.
What interviewers look for
- Minimal state and a derived pipeline: sort, then paginate.
- Not mutating the source array.
- Number-aware comparison.
- Resetting the page when it would go out of range.
- The order of operations when filtering is added: filter → sort → paginate.
Reference solution
src/App.js
import { useMemo, useState } from 'react'
import { users } from './users'
const columns = [
{ key: 'name', label: 'Name' },
{ key: 'age', label: 'Age' },
{ key: 'city', label: 'City' },
{ key: 'role', label: 'Role' },
]
const PAGE_SIZES = [5, 10, 20]
function compare(a, b) {
return typeof a === 'number' && typeof b === 'number' ? a - b : String(a).localeCompare(b)
}
export default function App() {
const [sort, setSort] = useState({ key: null, direction: 'ascending' })
const [pageSize, setPageSize] = useState(10)
const [page, setPage] = useState(1)
// Sorting copies the array: `users` itself is never mutated.
const sorted = useMemo(() => {
if (!sort.key) return users
const sign = sort.direction === 'ascending' ? 1 : -1
return [...users].sort((a, b) => sign * compare(a[sort.key], b[sort.key]))
}, [sort])
const pageCount = Math.max(1, Math.ceil(sorted.length / pageSize))
const rows = sorted.slice((page - 1) * pageSize, page * pageSize)
function toggleSort(key) {
setSort((current) =>
current.key === key && current.direction === 'ascending'
? { key, direction: 'descending' }
: { key, direction: 'ascending' },
)
setPage(1)
}
function changePageSize(size) {
setPageSize(size)
setPage(1)
}
return (
<main className="app">
<table>
<thead>
<tr>
{columns.map((column) => {
const active = sort.key === column.key
return (
<th key={column.key} aria-sort={active ? sort.direction : 'none'}>
<button
type="button"
className="sort-button"
onClick={() => toggleSort(column.key)}
>
{column.label}
<span aria-hidden="true">
{active ? (sort.direction === 'ascending' ? '▲' : '▼') : '↕'}
</span>
</button>
</th>
)
})}
</tr>
</thead>
<tbody>
{rows.map((user) => (
<tr key={user.id}>
{columns.map((column) => (
<td key={column.key}>{user[column.key]}</td>
))}
</tr>
))}
</tbody>
</table>
<nav className="pagination" aria-label="Pagination">
<button type="button" onClick={() => setPage(page - 1)} disabled={page === 1}>
Previous
</button>
<span aria-live="polite">
Page {page} of {pageCount}
</span>
<button type="button" onClick={() => setPage(page + 1)} disabled={page === pageCount}>
Next
</button>
<label>
Rows per page{' '}
<select
value={pageSize}
onChange={(event) => changePageSize(Number(event.target.value))}
>
{PAGE_SIZES.map((size) => (
<option key={size} value={size}>
{size}
</option>
))}
</select>
</label>
</nav>
</main>
)
}