Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Jan 21, 2025
1 parent 194d2bd commit 08092fa
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/yqlib/doc/operators/headers/sort-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ diff file1.yml file2.yml

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.

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`.
See [here](https://mikefarah.gitbook.io/yq/operators/entries#custom-sort-map-keys) for an example.
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)`.

4 changes: 2 additions & 2 deletions pkg/yqlib/doc/operators/sort-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ diff file1.yml file2.yml

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.

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`.
See [here](https://mikefarah.gitbook.io/yq/operators/entries#custom-sort-map-keys) for an example.
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)`.


## Sort keys of map
Given a sample.yml file of:
Expand Down
40 changes: 40 additions & 0 deletions pkg/yqlib/doc/operators/sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,46 @@ cool:
- c: banana
```
## Sort a map
Sorting a map, by default, will sort by the values
Given a sample.yml file of:
```yaml
y: b
z: a
x: c
```
then
```bash
yq 'sort' sample.yml
```
will output
```yaml
z: a
y: b
x: c
```
## Sort a map by keys
Use sort_by to sort a map using a custom function
Given a sample.yml file of:
```yaml
Y: b
z: a
x: c
```
then
```bash
yq 'sort_by(key | downcase)' sample.yml
```
will output
```yaml
x: c
Y: b
z: a
```
## Sort is stable
Note the order of the elements in unchanged when equal in sorting.
Expand Down
1 change: 0 additions & 1 deletion pkg/yqlib/operator_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func sortByOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
if candidate.Kind == MappingNode {

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

originalNode := candidate.Content[i]
Expand Down
18 changes: 18 additions & 0 deletions pkg/yqlib/operator_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ var sortByOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::cool: [{a: banana}, {b: banana}, {c: banana}]\n",
},
},
{
description: "Sort a map",
subdescription: "Sorting a map, by default, will sort by the values",
document: "y: b\nz: a\nx: c\n",
expression: `sort`,
expected: []string{
"D0, P[], (!!map)::z: a\ny: b\nx: c\n",
},
},
{
description: "Sort a map by keys",
subdescription: "Use sort_by to sort a map using a custom function",
document: "Y: b\nz: a\nx: c\n",
expression: `sort_by(key | downcase)`,
expected: []string{
"D0, P[], (!!map)::x: c\nY: b\nz: a\n",
},
},
{
description: "Sort is stable",
subdescription: "Note the order of the elements in unchanged when equal in sorting.",
Expand Down

0 comments on commit 08092fa

Please sign in to comment.