yq/pkg/yqlib/doc/operators/sort.md

154 lines
2.5 KiB
Markdown
Raw Normal View History

2021-12-04 02:54:12 +00:00
# Sort
2021-12-04 23:53:37 +00:00
Sorts an array. Use `sort` to sort an array as is, or `sort_by(exp)` to sort by a particular expression (e.g. subfield).
2021-12-04 02:54:12 +00:00
2022-02-22 03:15:31 +00:00
To sort by descending order, pipe the results through the `reverse` operator after sorting.
2021-12-04 02:54:12 +00:00
Note that at this stage, `yq` only sorts scalar fields.
2022-02-22 03:15:31 +00:00
2021-12-04 02:54:12 +00:00
## Sort by string field
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
[{a: banana}, {a: cat}, {a: apple}]
2021-12-04 02:54:12 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'sort_by(.a)' sample.yml
2021-12-04 02:54:12 +00:00
```
will output
```yaml
2023-05-09 03:51:21 +00:00
[{a: apple}, {a: banana}, {a: cat}]
2021-12-04 02:54:12 +00:00
```
## Sort by multiple fields
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
[{a: dog}, {a: cat, b: banana}, {a: cat, b: apple}]
```
then
```bash
yq 'sort_by(.a, .b)' sample.yml
```
will output
```yaml
2023-05-09 03:51:21 +00:00
[{a: cat, b: apple}, {a: cat, b: banana}, {a: dog}]
```
2022-02-22 03:15:31 +00:00
## Sort descending by string field
Use sort with reverse to sort in descending order.
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
[{a: banana}, {a: cat}, {a: apple}]
2022-02-22 03:15:31 +00:00
```
then
```bash
yq 'sort_by(.a) | reverse' sample.yml
```
will output
```yaml
2023-05-09 03:51:21 +00:00
[{a: cat}, {a: banana}, {a: apple}]
2022-02-22 03:15:31 +00:00
```
2021-12-04 23:53:37 +00:00
## Sort array in place
Given a sample.yml file of:
```yaml
cool:
- a: banana
- a: cat
- a: apple
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.cool |= sort_by(.a)' sample.yml
2021-12-04 23:53:37 +00:00
```
will output
```yaml
cool:
- a: apple
- a: banana
- a: cat
```
## Sort array of objects by key
Note that you can give sort_by complex expressions, not just paths
Given a sample.yml file of:
```yaml
cool:
- b: banana
- a: banana
- c: banana
```
then
```bash
2022-01-27 06:21:10 +00:00
yq '.cool |= sort_by(keys | .[0])' sample.yml
2021-12-04 23:53:37 +00:00
```
will output
```yaml
cool:
- a: banana
- b: banana
- c: banana
```
2021-12-04 02:54:12 +00:00
## Sort is stable
Note the order of the elements in unchanged when equal in sorting.
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
[{a: banana, b: 1}, {a: banana, b: 2}, {a: banana, b: 3}, {a: banana, b: 4}]
2021-12-04 02:54:12 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'sort_by(.a)' sample.yml
2021-12-04 02:54:12 +00:00
```
will output
```yaml
2023-05-09 03:51:21 +00:00
[{a: banana, b: 1}, {a: banana, b: 2}, {a: banana, b: 3}, {a: banana, b: 4}]
2021-12-04 02:54:12 +00:00
```
## Sort by numeric field
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
[{a: 10}, {a: 100}, {a: 1}]
2021-12-04 02:54:12 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'sort_by(.a)' sample.yml
2021-12-04 02:54:12 +00:00
```
will output
```yaml
2023-05-09 03:51:21 +00:00
[{a: 1}, {a: 10}, {a: 100}]
2021-12-04 02:54:12 +00:00
```
2022-11-04 01:21:12 +00:00
## Sort by custom date field
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
[{a: 12-Jun-2011}, {a: 23-Dec-2010}, {a: 10-Aug-2011}]
2022-11-04 01:21:12 +00:00
```
then
```bash
yq 'with_dtf("02-Jan-2006"; sort_by(.a))' sample.yml
```
will output
```yaml
2023-05-09 03:51:21 +00:00
[{a: 23-Dec-2010}, {a: 12-Jun-2011}, {a: 10-Aug-2011}]
2022-11-04 01:21:12 +00:00
```
2021-12-04 02:54:12 +00:00
## Sort, nulls come first
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
[8, 3, null, 6, true, false, cat]
2021-12-04 02:54:12 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'sort' sample.yml
2021-12-04 02:54:12 +00:00
```
will output
```yaml
2023-05-09 03:51:21 +00:00
[null, false, true, 3, 6, 8, cat]
2021-12-04 02:54:12 +00:00
```