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

35 lines
473 B
Markdown
Raw Normal View History

2022-02-22 03:15:31 +00:00
# Reverse
Reverses the order of the items in an array
## Reverse
Given a sample.yml file of:
```yaml
2023-05-09 03:51:21 +00:00
[1, 2, 3]
2022-02-22 03:15:31 +00:00
```
then
```bash
yq 'reverse' sample.yml
```
will output
```yaml
2023-05-09 03:51:21 +00:00
[3, 2, 1]
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
```