# Choose which keys come first

Name the keys that decide first between patterns that match as many properties, with the order option.

Use this when, of two patterns that both match with as many properties, the one with a certain key should win, whatever the alphabet says. Name those keys in the `order` option: they come first, in the order you give, and every other key comes after them in alphabetical order.

```js
const patrun = require('patrun')

const plain = patrun()
  .add({ cmd: 'list' }, 'CMD')
  .add({ srv: 'user' }, 'SRV')

plain.find({ cmd: 'list', srv: 'user' }) // → 'CMD'  cmd comes before srv

const pm = patrun({ order: ['srv'] })
  .add({ cmd: 'list' }, 'CMD')
  .add({ srv: 'user' }, 'SRV')

pm.find({ cmd: 'list', srv: 'user' }) // → 'SRV'  srv comes first
```

With `{ order: ['srv', 'role'] }` the key order is `srv`, then `role`, then the rest alphabetically.

The key order is all that the option changes. A pattern with more properties still wins, whatever its keys:

```js
pm.add({ cmd: 'list', role: 'admin' }, 'CMD-ROLE')

pm.find({ cmd: 'list', role: 'admin', srv: 'user' }) // → 'CMD-ROLE'
```

Of two patterns with the same keys, the one with an exact value at the first key in the order wins over one with `'*'` there:

```js
const patrun = require('patrun')

const stars = patrun({ order: ['srv'] })
  .add({ cmd: 'list', srv: '*' }, 'ANY-SRV')
  .add({ cmd: '*', srv: 'user' }, 'ANY-CMD')

stars.find({ cmd: 'list', srv: 'user' }) // → 'ANY-CMD'  exact at srv
```

With no order the first key is `cmd`, and the answer is `'ANY-SRV'`.

## In Go

Name the keys in `Options.Order`:

```go
pm := patrun.New(patrun.Options{Order: []string{"srv"}})
pm.Add(map[string]string{"cmd": "list"}, s("CMD"))
pm.Add(map[string]string{"srv": "user"}, s("SRV"))

pm.Find(map[string]string{"cmd": "list", "srv": "user"}) // → "SRV"
```

`New` copies the slice, so changing it afterwards changes nothing.

## What to watch for

A router keeps the order it was made with. The order names each key once, and no key is the empty string: for any other order, `patrun` throws a `TypeError` and Go's `New` panics. It may name keys that no pattern has, which changes nothing.

The same order decides how `collect` and `list` give patterns, and the order of the keys `toString` prints. The [JavaScript reference](https://rjrodger.github.io/patrun/reference/javascript.html#patruncustom) and the [Go reference](https://rjrodger.github.io/patrun/reference/go.html#options) state the option in full.
