Skip to content

Sort array of dependencies in environment.yml #1332

Answered by mikefarah
rcasero asked this question in Q&A
Discussion options

You must be logged in to vote

This is a little trickier than examples I have in the docs, because in that array of strings there is a single map entry pip: ... - so the vanilla sort will complain about not knowing how to sort a map ( sort only works for scalars, got !!map)

What needs to be done is to use the sort_by operator to specify how to sort this array - particularly when it encounters a map.

Solving this actually revealed a bug in the alternative (//) operator in yq! I've made a new release v4.27.4 - so you will need that version for this:

yq '.dependencies |= sort_by( select(tag == "!!str") //  (keys | .[0]) )' file.yaml

Explanation:

  • we want to update .dependencies based on its current value |=
  • we're going t…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@rcasero
Comment options

@mikefarah
Comment options

@rcasero
Comment options

Answer selected by rcasero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants