mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-05 17:36:59 +08:00
Added reverse op
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
|
||||
Sorts an array. Use `sort` to sort an array as is, or `sort_by(exp)` to sort by a particular expression (e.g. subfield).
|
||||
|
||||
To sort by descending order, pipe the results through the `reverse` operator after sorting.
|
||||
|
||||
Note that at this stage, `yq` only sorts scalar fields.
|
||||
|
||||
|
||||
{% hint style="warning" %}
|
||||
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
||||
|
||||
@@ -28,6 +31,26 @@ will output
|
||||
- a: cat
|
||||
```
|
||||
|
||||
## Sort descending by string field
|
||||
Use sort with reverse to sort in descending order.
|
||||
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
- a: banana
|
||||
- a: cat
|
||||
- a: apple
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq 'sort_by(.a) | reverse' sample.yml
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
- a: cat
|
||||
- a: banana
|
||||
- a: apple
|
||||
```
|
||||
|
||||
## Sort array in place
|
||||
Given a sample.yml file of:
|
||||
```yaml
|
||||
|
||||
Reference in New Issue
Block a user