From d04a5b0871f289c0f6dd2bb284a08875acaa435d Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Thu, 11 Sep 2025 20:09:10 +0200 Subject: [PATCH] chore(jq): sort by key or value --- snippets/jq.fish | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/snippets/jq.fish b/snippets/jq.fish index 04c0d8d..15ba9a1 100644 --- a/snippets/jq.fish +++ b/snippets/jq.fish @@ -114,3 +114,17 @@ yq -e '(.backend.url|test("^file://")?)|not' 'Pulumi.yaml' # Apply formatting to the same file you read from yq -iY --explicit-start '.' 'external-snapshotter/crds.yml' + +# Sort +# Refer +# by key +jq -r --sort-keys '.' 'file.json' +jq -r 'to_entries | sort_by(.key) | from_entries' 'file.json' +# by value +jq -r 'to_entries | sort_by(.value) | from_entries' 'file.json' +# array, by 'age' +jq -r 'sort_by(.age)' 'file.json' +# array, by 'age', limit to the first 3 elements +jq -r 'sort_by(.age)|[limit(3;.[])]' 'file.json' +# array, by 'age' descending +jq -r 'sort_by(.age)|reverse' 'file.json'