Hacker News new | ask | show | jobs
by HellsMaddy 822 days ago
Jq tip: Instead of `sort_by(.count) | reverse`, you can do `sort_by(-.count)`
1 comments

only if you're sure that .count is never null:

  $ echo '[{"a": {"count": null}}]' | jq -c 'sort_by(-.count)'
  jq: error (at <stdin>:1): null (null) cannot be negated
  $ echo '[{"a": {"count": null}}]' | jq -c 'sort_by(.count) | reverse'
  [{"a":{"count":null}}]
this whole thread is like nerd sniping me :-D but I felt compelled to draw attention to jq's coalesce operator because I only recently learned about it and searching for the word "coalesce" in the man page is pfffft (it's official name is "Alternative operator", with alternative being "for false and null")

  $ echo '[{"a": {"count": null}}]' | jq -c 'sort_by(-(.count//0))'
  [{"a":{"count":null}}]