From 6db7acbf69c5cf15e367c2518099caf8b27ed8eb Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 17 Oct 2022 15:03:47 +1100 Subject: [PATCH] Added custom sort map example --- pkg/yqlib/doc/operators/entries.md | 20 ++++++++++++++++++++ pkg/yqlib/doc/operators/headers/sort-keys.md | 3 +++ pkg/yqlib/doc/operators/sort-keys.md | 3 +++ pkg/yqlib/operator_entries_test.go | 9 +++++++++ 4 files changed, 35 insertions(+) diff --git a/pkg/yqlib/doc/operators/entries.md b/pkg/yqlib/doc/operators/entries.md index b9d1ae9d..3c799b4c 100644 --- a/pkg/yqlib/doc/operators/entries.md +++ b/pkg/yqlib/doc/operators/entries.md @@ -107,6 +107,26 @@ KEY_a: 1 KEY_b: 2 ``` +## Custom sort map keys +Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back. + +Given a sample.yml file of: +```yaml +a: 1 +c: 3 +b: 2 +``` +then +```bash +yq 'to_entries | sort_by(.key) | reverse | from_entries' sample.yml +``` +will output +```yaml +c: 3 +b: 2 +a: 1 +``` + ## Use with_entries to filter the map Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/doc/operators/headers/sort-keys.md b/pkg/yqlib/doc/operators/headers/sort-keys.md index 0ef5cd23..31b0b6df 100644 --- a/pkg/yqlib/doc/operators/headers/sort-keys.md +++ b/pkg/yqlib/doc/operators/headers/sort-keys.md @@ -11,3 +11,6 @@ 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 your 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. diff --git a/pkg/yqlib/doc/operators/sort-keys.md b/pkg/yqlib/doc/operators/sort-keys.md index b5101dc1..36c605d2 100644 --- a/pkg/yqlib/doc/operators/sort-keys.md +++ b/pkg/yqlib/doc/operators/sort-keys.md @@ -12,6 +12,9 @@ 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 your 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. + {% hint style="warning" %} Note that versions prior to 4.18 require the 'eval/e' command to be specified. diff --git a/pkg/yqlib/operator_entries_test.go b/pkg/yqlib/operator_entries_test.go index 1419391d..9313323d 100644 --- a/pkg/yqlib/operator_entries_test.go +++ b/pkg/yqlib/operator_entries_test.go @@ -52,6 +52,15 @@ var entriesOperatorScenarios = []expressionScenario{ "D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n", }, }, + { + description: "Custom sort map keys", + 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.", + document: `{a: 1, c: 3, b: 2}`, + expression: `to_entries | sort_by(.key) | reverse | from_entries`, + expected: []string{ + "D0, P[], (!!map)::c: 3\nb: 2\na: 1\n", + }, + }, { skipDoc: true, document: `{a: 1, b: 2}`,