Skip to content

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

Open
@robbat2

Description

@robbat2

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions