# Sort Controls

## Objective

Let users choose the order, and keep that order stable.

Sortable columns and sort menus whose order is deterministic, shareable in the URL, and safe against injected field names.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Define an explicit allowlist of sortable fields and map incoming sort parameters through it. Anything not on the list falls back to the default.
2. Always append a unique tie-breaker, such as the primary key, to every sort. Without it, records with equal values reshuffle between pages and users see duplicates and gaps.
3. Put the active sort field and direction in the URL so the view can be shared, bookmarked, and restored on back-navigation.
4. Decide where nulls and blanks go and apply that decision everywhere — missing values should not silently land at whichever end the database prefers.
5. Do NOT interpolate a client-supplied field or direction into a query. That is a direct injection path, and an allowlist costs nothing.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Text sorting must be case-insensitive and locale-aware, or lowercase entries pile up after uppercase ones.
- Numbers stored as strings sort as strings. Cast, or store them as numbers.
- Changing the sort must reset to page one; keeping the offset lands the user mid-list in a different order.
- A sort over a computed or joined value needs an index, or it will quietly become a full table scan.
- The active sort and direction must be exposed to assistive tech via aria-sort, not by an arrow glyph alone.
- The default sort must be explicit. 'Whatever the database returns' is not an order and will change.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Only allowlisted fields can be sorted; unknown values fall back to the default.
- [ ] Every sort includes a unique tie-breaker and paginates without repeats or gaps.
- [ ] Sort state is in the URL and restores on reload and back-navigation.
- [ ] Null and blank placement is consistent and documented.
- [ ] Changing sort returns to the first page.
- [ ] Sortable headers expose their state through aria-sort.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
