4 ms·
Jq tip: Instead of `sort_by(.count) | reverse`, you can do `sort_by(-.count)`
by HellsMaddy 3y ago
Jq tip: Instead of `sort_by(.count) | reverse`, you can do `sort_by(-.count)`
- philsnow 3y agoonly 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}}]
- mdaniel 3y agothis 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}}]