Skip to content

Commit 6db7acb

Browse files
committed
Added custom sort map example
1 parent d16ee38 commit 6db7acb

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

pkg/yqlib/doc/operators/entries.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ KEY_a: 1
107107
KEY_b: 2
108108
```
109109
110+
## Custom sort map keys
111+
Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.
112+
113+
Given a sample.yml file of:
114+
```yaml
115+
a: 1
116+
c: 3
117+
b: 2
118+
```
119+
then
120+
```bash
121+
yq 'to_entries | sort_by(.key) | reverse | from_entries' sample.yml
122+
```
123+
will output
124+
```yaml
125+
c: 3
126+
b: 2
127+
a: 1
128+
```
129+
110130
## Use with_entries to filter the map
111131
Given a sample.yml file of:
112132
```yaml

pkg/yqlib/doc/operators/headers/sort-keys.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ diff file1.yml file2.yml
1111
```
1212

1313
Note that `yq` does not yet consider anchors when sorting by keys - this may result in invalid yaml documents if your are using merge anchors.
14+
15+
For more advanced sorting, using `to_entries` to convert the map to an array, then sort/process the array as you like (e.g. using `sort_by`) and convert back to a map using `from_entries`.
16+
See [here](https://mikefarah.gitbook.io/yq/operators/entries#custom-sort-map-keys) for an example.

pkg/yqlib/doc/operators/sort-keys.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ diff file1.yml file2.yml
1212

1313
Note that `yq` does not yet consider anchors when sorting by keys - this may result in invalid yaml documents if your are using merge anchors.
1414

15+
For more advanced sorting, using `to_entries` to convert the map to an array, then sort/process the array as you like (e.g. using `sort_by`) and convert back to a map using `from_entries`.
16+
See [here](https://mikefarah.gitbook.io/yq/operators/entries#custom-sort-map-keys) for an example.
17+
1518
{% hint style="warning" %}
1619
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
1720

pkg/yqlib/operator_entries_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ var entriesOperatorScenarios = []expressionScenario{
5252
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n",
5353
},
5454
},
55+
{
56+
description: "Custom sort map keys",
57+
subdescription: "Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.",
58+
document: `{a: 1, c: 3, b: 2}`,
59+
expression: `to_entries | sort_by(.key) | reverse | from_entries`,
60+
expected: []string{
61+
"D0, P[], (!!map)::c: 3\nb: 2\na: 1\n",
62+
},
63+
},
5564
{
5665
skipDoc: true,
5766
document: `{a: 1, b: 2}`,

0 commit comments

Comments
 (0)