# Keep a table's rows in the order a person chooses (/build/order-rows)

Name the column a hand-kept table's rows read in, keep an order a person drags by writing one row a move, and change or clear the order.



A `core.table` item keeps its own [table](/build/tables), and its `order` property names the column its rows read in. Every app that reads the rows without a `sort` then shows them in that order. With no `order`, rows read by `id`, which for the IDs Marfa makes is the order the rows were created in.

## Name the column [#name-the-column]

`order` names one of the item's columns of type `string`, `number`, `integer` or `datetime`. This reading list keeps its order in a `string` column named `position`:



**CLI**

```bash  
marfa items create --type core.table --properties '{"title":"Reading list",
  "columns":{"book":{"type":"string"},"position":{"type":"string"}},
  "order":"position"}'
```

**HTTP**

```http
POST /items
Authorization: Bearer <key>
Content-Type: application/json

{ "type": "core.table", "properties": { "title": "Reading list",
  "columns": { "book": { "type": "string" }, "position": { "type": "string" } },
  "order": "position" } }
```

A [read of the rows](/build/read-rows#read-a-page-of-rows) that names no `sort` answers them as `sort=values.position` would, on the server and in a [working copy](/build/working-copies). A read that names a `sort` still gets that sort.

## Keep the order a person drags [#keep-the-order-a-person-drags]

**Give each row a sort key that places it among the others,** such as `a`, `b` and `c`. To move a row between two others, patch its key to a string that sorts between theirs. This moves a row between the rows whose keys are `a` and `b`:

```bash
marfa rows patch <id> table <row> --values '{"position":"a5"}'
```

**A move writes one row,** and no other row changes. Keys compare by Unicode code point, so capital letters sort before small ones. Build every key the same way, such as all in small letters, so the rows read in the order you expect.

**Don't store a row number.** The number an app shows beside a row is its place in the read. A column that held it would need every row after a moved one written again.

## Change or clear the order [#change-or-clear-the-order]

**Naming, changing and clearing `order` needs no `schema.write`:** any key that may write the item may do it. To clear it, name `order` in `clear_properties`, or pass `--clear-property order` to `marfa items update`. The rows then read by `id`.

A type below `core.table` that declares an `order` field of its own keeps that field for its own meaning, and its rows don't read in that order.

## When Marfa refuses an order [#when-marfa-refuses-an-order]

* **An `order` that names no column of a type it can sort by** is refused `400 invalid_properties`, naming `order` in `details.errors`. A `date` column is not one.
* **A write that removes the column that is the order, or changes its type,** is refused the same way. Name another `order` in the same write, or clear `order`.
* **An item can hold an `order` that names no such column,** such as one written before its type came under `core.table`. Marfa keeps the value, reads the rows as if there were no `order`, and carries it through an export and a restore. A write that sets an `order` naming no such column is refused. So is a write of `columns` that doesn't also name a valid `order` or clear it.
* **A type change that would bring a type under `core.table`, or drop the `order` field of a type already there,** is refused `400 invalid_schema`, even with `force=true`, while an item holds an `order` that names none of its columns. Correct or clear those orders first.

The rules are in the contract's [tables](https://github.com/withmarfa/marfa/blob/main/conformance/spec/tables.md#tablesown-order) chapter.
