Skip to content

Instantly share code, notes, and snippets.

@sloanlance
Created July 12, 2017 20:40
Show Gist options
  • Save sloanlance/ae54cf055e3d7713a9b76c1abe2d8292 to your computer and use it in GitHub Desktop.
Save sloanlance/ae54cf055e3d7713a9b76c1abe2d8292 to your computer and use it in GitHub Desktop.
jq: Functions to sort the *properties* of an object by key or value. I believe these are largely part of the recent versions of jq, but I think they're interesting. From the poorly-named repo https://github.com/joelpurra/jq-object-sorting (which sorts properties, not objects).
def byKeyAsc:
to_entries
| sort_by(.key)
| from_entries;
def byKeyDesc:
to_entries
| sort_by(.key)
| reverse
| from_entries;
def byValueAsc:
to_entries
| sort_by(.value)
| from_entries;
def byValueDesc:
to_entries
| sort_by(.value)
| reverse
| from_entries;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment