Skip to content

Commit 08092fa

Browse files
committed
wip
1 parent 194d2bd commit 08092fa

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ 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 you 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.
15+
For more advanced sorting, you can use the [sort_by](https://mikefarah.gitbook.io/yq/operators/sort) function on a map, and give it a custom function like `sort_by(key | downcase)`.
16+

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ 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 you 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.
15+
For more advanced sorting, you can use the [sort_by](https://mikefarah.gitbook.io/yq/operators/sort) function on a map, and give it a custom function like `sort_by(key | downcase)`.
16+
1717

1818
## Sort keys of map
1919
Given a sample.yml file of:

pkg/yqlib/doc/operators/sort.md

+40
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,46 @@ cool:
109109
- c: banana
110110
```
111111
112+
## Sort a map
113+
Sorting a map, by default, will sort by the values
114+
115+
Given a sample.yml file of:
116+
```yaml
117+
y: b
118+
z: a
119+
x: c
120+
```
121+
then
122+
```bash
123+
yq 'sort' sample.yml
124+
```
125+
will output
126+
```yaml
127+
z: a
128+
y: b
129+
x: c
130+
```
131+
132+
## Sort a map by keys
133+
Use sort_by to sort a map using a custom function
134+
135+
Given a sample.yml file of:
136+
```yaml
137+
Y: b
138+
z: a
139+
x: c
140+
```
141+
then
142+
```bash
143+
yq 'sort_by(key | downcase)' sample.yml
144+
```
145+
will output
146+
```yaml
147+
x: c
148+
Y: b
149+
z: a
150+
```
151+
112152
## Sort is stable
113153
Note the order of the elements in unchanged when equal in sorting.
114154

pkg/yqlib/operator_sort.go

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func sortByOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
2929
if candidate.Kind == MappingNode {
3030

3131
sortableArray = make(sortableNodeArray, len(candidate.Content)/2)
32-
log.Warningf("Sorting map: %v", NodeToString(candidate))
3332
for i := 1; i < len(candidate.Content); i = i + 2 {
3433

3534
originalNode := candidate.Content[i]

pkg/yqlib/operator_sort_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ var sortByOperatorScenarios = []expressionScenario{
8484
"D0, P[], (!!map)::cool: [{a: banana}, {b: banana}, {c: banana}]\n",
8585
},
8686
},
87+
{
88+
description: "Sort a map",
89+
subdescription: "Sorting a map, by default, will sort by the values",
90+
document: "y: b\nz: a\nx: c\n",
91+
expression: `sort`,
92+
expected: []string{
93+
"D0, P[], (!!map)::z: a\ny: b\nx: c\n",
94+
},
95+
},
96+
{
97+
description: "Sort a map by keys",
98+
subdescription: "Use sort_by to sort a map using a custom function",
99+
document: "Y: b\nz: a\nx: c\n",
100+
expression: `sort_by(key | downcase)`,
101+
expected: []string{
102+
"D0, P[], (!!map)::x: c\nY: b\nz: a\n",
103+
},
104+
},
87105
{
88106
description: "Sort is stable",
89107
subdescription: "Note the order of the elements in unchanged when equal in sorting.",

0 commit comments

Comments
 (0)