Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yq --sort-keys options, like jq #2087

Open
robbat2 opened this issue Jun 30, 2024 · 0 comments
Open

yq --sort-keys options, like jq #2087

robbat2 opened this issue Jun 30, 2024 · 0 comments

Comments

@robbat2
Copy link

robbat2 commented Jun 30, 2024

Please describe your feature request.
TL;DR: Please add a yq --sort-keys option, like jq --sort-keys.

jq has a --sort-keys option, that sorts the keys in the output consistently.

I use it in shell commands like:

diff -NuarwbB <(jq --sort-keys $EXPR file-A) <(jq --sort-keys $EXPR file-B)

To be able to see the differences quickly regardless of any formatting or ordering of hashes.

It should sort all hashes by the lexical order of their keys; for the entire file, regardless of depth.

This is available via sort_keys(..) as documented in the manpage, but how about the command-line option for ease of use, esp. with other complex expressions.

Describe the solution you'd like
If we have data1.yml like:
(please keep to around 10 lines )

hash: 
  keyZ: Zambia
  keyA: Australia

And we run a command:

yq -P --sort-keys . 

it should output

hash:
  keyA: Australia
  keyZ: Zambia

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Existing workarounds:

  • Convert to json, sort in JQ, convert back to YAML. This loses comments, anchors, references.
  • yq $EXPR file-A | yq -P 'sort_keys(..)' => extra exec, sometimes forget about it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant