Sort Builder

The multi-key sort editor: an ordered list of "key, direction" rows — sort by status A → Z, then by created newest first — that a person builds, reorders and shares, plus the pure functions that turn that list into a comparator, a URL parameter, server JSON and a sentence. Reach for it wherever one sort arrow on one column has stopped being enough: the sort panel of an admin or data table, a saved or shared view, the order of a report or a CSV/Excel export, a scheduled or emailed report, the "order by" a person composes for an API or a query builder, a leaderboard or ranking screen, a kanban or backlog whose rows need priority then due date, a search-results ordering control, a pricing or product grid sorted by two things at once, a log or audit viewer, a file or media browser (name sorted naturally, so step-9 comes before step-10), and anywhere a list must look the same on every refresh instead of quietly reshuffling. Different from this registry's sort-header, which is the table heading you click to sort by one column in three states — that one is a single column's ascending/descending/off, this one is the panel where several keys are stacked in priority order, each with its own direction, and the order of the keys is the meaning. What it gets right that a hand-rolled comparator usually does not: a key can only appear once (a second row on the same key cannot change the order, so it is never offered); text is compared through a cached Intl.Collator rather than with <, because codepoint order files every lowercase word after every uppercase one and sends Ähre and ångström past Zebra, and because the locale genuinely changes the answer (Swedish puts å ä ö after z); natural ordering for digits inside strings is opt-in per key (item9 before item10); missing values are partitioned out before any comparison, since Array.prototype.sort pins undefined to the end without ever calling the comparator — so a blanks-first setting drawn on screen would silently do nothing — and null left to < sorts where the word "null" falls; the blanks position is held separately from the direction and is deliberately not flipped by it, because SQL engines disagree (PostgreSQL treats null as largest, MySQL as smallest) and reversing a sort should not throw every empty row to the other end of the table; a value that cannot be read as a date or a number is treated as blank rather than fed to a subtraction, because a NaN from a comparator does not throw and does not sort, it leaves the whole table in the order it arrived with no error anywhere; and there is a tie-break key applied last, which is not about sort stability — Array.prototype.sort has been stable since ES2019 — but because stability preserves the order the rows arrived in, and two fetches of the same equal-ranked rows do not arrive in the same order. Four outputs, so the same list can drive the client and the server: sortRows/sortComparator for sorting in the browser (with per-key accessors, so a column showing "3 days ago" sorts on the timestamp); buildSortQuery for the JSON to send, which resolves each key's blanks position and hands back every term it had to drop (unfinished, unknown or duplicated) instead of silently skipping it; toSortParam/parseSortParam for the URL, in the ?sort=-created,name form JSON:API specifies, escaped so a key containing a comma or starting with a hyphen round-trips, and returning null for a parameter it cannot fully read rather than applying half an order; and describeSort for the live region, the summary line or a chip. Accessible by construction: an ordered list so position is conveyed rather than implied, a named control per row ("Direction for sort key 2"), direction wording that follows the kind (newest first / largest first / A → Z), move-earlier and move-later buttons that use aria-disabled rather than disabled so the tab order does not shift and focus is not dropped when a key reaches the end, a polite live region for added, removed and moved, and a stale key kept visible and flagged instead of being silently re-pointed at another column. Common asks it answers: "multi sort react", "multiple column sort react", "sort builder react", "multi key sort component", "shadcn sort builder", "shadcn multi sort", "order by builder ui", "sort panel react table", "tanstack table multi sort ui", "secondary sort react", "sort by then by ui", "priority sort list react", "reorder sort keys ui", "drag sort priority react", "sort state in url", "serialize sort to query string", "parse sort query param", "json api sort parameter", "?sort=-created", "shareable sorted link", "send sort to api json", "sort to order by sql", "nulls first nulls last react", "where do empty values go when sorting", "blank rows sort to top", "intl collator react sort", "localeCompare vs Intl.Collator", "accented characters sort wrong javascript", "natural sort react", "sort item10 before item9", "alphanumeric natural order javascript", "stable sort react table", "rows reshuffle on refresh sort", "tie breaker sort javascript", "sort by date newest first react", "invalid date sort nan", "sort not working silently javascript", "sort comparator returns nan". Styled entirely with shadcn tokens (input, ring, accent, destructive, muted-foreground, background) so it follows light and dark mode, controlled or uncontrolled, and its only npm dependency is lucide-react for the four icons — no table library, no sort library, one file.

Usage docs and preview are being generated by the coding agent. Registry metadata is shown meanwhile.
building preview…
The live preview has not been built yet.
See how it was built

Installation

pnpm dlx shadcn@latest add @pulld/sort-builder

Requires @pulld in the registries of your components.json (shadcn adds official directory namespaces automatically).

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "Sort Builder" component (pulld/sort-builder) from its shadcn registry.1. Install it with: npx shadcn@latest add @pulld/sort-builder2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.

Files & dependencies

  • registry/ui/sort-builder.tsx
dependencieslucide-react

Looks similar, elsewhere

There is no screenshot of this item to compare yet.