yq
v4.x
v4.x
  • yq
  • How It Works
  • Recipes
  • Upgrading from V3
  • Commands
    • Evaluate
    • Evaluate All
    • Shell Completion
  • Operators
    • Add
    • Alternative (Default value)
    • Anchor and Alias Operators
    • Array to Map
    • Assign (Update)
    • Boolean Operators
    • Collect into Array
    • Column
    • Comment Operators
    • Compare Operators
    • Contains
    • Create, Collect into Object
    • Date Time
    • Delete
    • Divide
    • Document Index
    • Encode / Decode
    • Entries
    • Env Variable Operators
    • Equals
    • Eval
    • File Operators
    • Filter Operator
    • Flatten
    • Group By
    • Has
    • Keys
    • Kind
    • Length
    • Line
    • Load
    • Min
    • Map
    • Max
    • Modulo
    • Multiply (Merge)
    • Omit
    • Parent
    • Path
    • Pick
    • Pipe
    • Pivot
    • Recursive Descent (Glob)
    • Reduce
    • Reverse
    • Select
    • Shuffle
    • Slice Array
    • Sort
    • Sort Keys
    • Split into Documents
    • String Operators
    • Style
    • Subtract
    • Tag
    • To Number
    • Traverse (Read)
    • Union
    • Unique
    • Variable Operators
    • With
  • Usage
    • Output format
    • Working with CSV, TSV
    • Working with JSON
    • Working with Properties
    • Working with XML
    • Working with LUA
    • Working with TOML
    • Working with Shell Output
    • Front Matter
    • Split into multiple files
    • GitHub Action
    • Tips, Tricks, Troubleshooting
  • Github Page
Powered by GitBook
On this page
  • Sort by string field
  • Sort by multiple fields
  • Sort descending by string field
  • Sort array in place
  • Sort array of objects by key
  • Sort a map
  • Sort a map by keys
  • Sort is stable
  • Sort by numeric field
  • Sort by custom date field
  • Sort, nulls come first

Was this helpful?

  1. Operators

Sort

Sorts an array. Use sort to sort an array as is, or sort_by(exp) to sort by a particular expression (e.g. subfield).

To sort by descending order, pipe the results through the reverse operator after sorting.

Note that at this stage, yq only sorts scalar fields.

Sort by string field

Given a sample.yml file of:

- a: banana
- a: cat
- a: apple

then

yq 'sort_by(.a)' sample.yml

will output

- a: apple
- a: banana
- a: cat

Sort by multiple fields

Given a sample.yml file of:

- a: dog
- a: cat
  b: banana
- a: cat
  b: apple

then

yq 'sort_by(.a, .b)' sample.yml

will output

- a: cat
  b: apple
- a: cat
  b: banana
- a: dog

Sort descending by string field

Use sort with reverse to sort in descending order.

Given a sample.yml file of:

- a: banana
- a: cat
- a: apple

then

yq 'sort_by(.a) | reverse' sample.yml

will output

- a: cat
- a: banana
- a: apple

Sort array in place

Given a sample.yml file of:

cool:
  - a: banana
  - a: cat
  - a: apple

then

yq '.cool |= sort_by(.a)' sample.yml

will output

cool:
  - a: apple
  - a: banana
  - a: cat

Sort array of objects by key

Note that you can give sort_by complex expressions, not just paths

Given a sample.yml file of:

cool:
  - b: banana
  - a: banana
  - c: banana

then

yq '.cool |= sort_by(keys | .[0])' sample.yml

will output

cool:
  - a: banana
  - b: banana
  - c: banana

Sort a map

Sorting a map, by default this will sort by the values

Given a sample.yml file of:

y: b
z: a
x: c

then

yq 'sort' sample.yml

will output

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:

Y: b
z: a
x: c

then

yq 'sort_by(key | downcase)' sample.yml

will output

x: c
Y: b
z: a

Sort is stable

Note the order of the elements in unchanged when equal in sorting.

Given a sample.yml file of:

- a: banana
  b: 1
- a: banana
  b: 2
- a: banana
  b: 3
- a: banana
  b: 4

then

yq 'sort_by(.a)' sample.yml

will output

- a: banana
  b: 1
- a: banana
  b: 2
- a: banana
  b: 3
- a: banana
  b: 4

Sort by numeric field

Given a sample.yml file of:

- a: 10
- a: 100
- a: 1

then

yq 'sort_by(.a)' sample.yml

will output

- a: 1
- a: 10
- a: 100

Sort by custom date field

Given a sample.yml file of:

- a: 12-Jun-2011
- a: 23-Dec-2010
- a: 10-Aug-2011

then

yq 'with_dtf("02-Jan-2006"; sort_by(.a))' sample.yml

will output

- a: 23-Dec-2010
- a: 12-Jun-2011
- a: 10-Aug-2011

Sort, nulls come first

Given a sample.yml file of:

- 8
- 3
- null
- 6
- true
- false
- cat

then

yq 'sort' sample.yml

will output

- null
- false
- true
- 3
- 6
- 8
- cat
PreviousSlice ArrayNextSort Keys

Last updated 2 months ago

Was this helpful?