JQ sort_by example

Sort a JSON array by a field using jq sort_by(). Interactive example showing how to sort objects ascending, then reverse for descending order.

The jq expression

.countries | sort_by(.population) | reverse | .[] | .name

sort_by(path) returns the input array sorted in ascending order by the value at path. jq only sorts ascending, so pipe the result to reverse to get descending order. Here we sort countries by .population from highest to lowest and output each country .name.