# Sorting a view

> Showing records in another order than they are kept in, without moving one, and without a row jumping as it is edited.

Sorting shows records in the order of one of their fields. It changes what
people see, not what is stored: the records stay as they are kept, and the view
remembers where each one is.

## Best practices

**Sort the view, not the data.** Sorting is a way of looking. It does not
reorder the records, is not a change to undo, and is not saved with them unless
the application chooses to remember it.

**Keep equal records in the order they had.** Sorting is stable: records with
the same value stay in the order they are kept in, so that sorting by one
column and then another gives what people expect.

**Put nothing last.** A record with no value for the field sorted by comes
after every record that has one — ascending and descending alike. Missing
values are not the smallest values; they are not values.

**Order each kind as it is understood.** Numbers by size; dates by time; text
as a person would file it, without regard to case or accent and with 2 before
10; a yes-or-no with no before yes; a choice in the order its options are
offered, which is the order that means something, not the order of their names.

**Do not move a row as it is edited.** When the value a row is sorted by is
changed, the row stays where it is until the view is sorted again. A row that
jumps away as it is typed in takes the person's place with it. Likewise a
record that is added is shown where it was added.

**Put back what is undone where it was shown.** Undoing a removal returns the
rows to the places they were shown in, under the sort they were removed under.

**Keep people's place.** Sorting keeps the active cell on its record, wherever
the record goes.

**Say what the order is.** The column sorted by shows it, and which way; the
change is said to those who cannot see it.

**Sort afresh when the data is replaced.** New records from elsewhere are
sorted as they arrive.

## Conventions

| Intent          | Pointer                        | Keyboard                           |
| --------------- | ------------------------------ | ---------------------------------- |
| Sort ascending  | Press the column's sort button | Alt-Shift-Up Arrow in the column   |
| Sort descending | Press it again                 | Alt-Shift-Down Arrow in the column |
| Stop sorting    | Press it a third time          | The same key again                 |

See also [Data grid](https://ui.publictheta.com/components/data-grid.md).

## Behaviour

Each statement is verified by a test in Chromium, Chromium (touch), Firefox, WebKit, WebKit (touch), unless it names the environments it applies to or is marked open.

### Sorting

- Records are shown in the order of the field sorted by, and every record is shown once.
- Records that compare equal keep the order they are kept in, whichever way the rest are sorted.
- A record with nothing in the field comes last, ascending and descending alike.
- Each kind is put in the order that means something for it.
- Where each record is shown is the order turned about.

### Through changes

- A change to a value moves nothing, even a value that is sorted by.
- Records added at the end are shown at the end, whatever they hold.
- Records put in among others are shown in front of the one they were put in front of.
- After any insertion or removal, every record still there is shown once, in the order it was in.

## Reference implementation

React: `npx shadcn@latest add https://ui.publictheta.com/r/view-order.json`
