Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort descending by multiple keys in jq

Tags:

I have the following array:

[{
    "name": "Object 1",
    "prop1": 5,
    "prop2": 2
}, {
    "name": "Object 2",
    "prop1": 6,
    "prop2": 4
}, {
    "name": "Object 3",
    "prop1": 5,
    "prop2": 3
}]

I want to sort this array analogous to this SQL ORDER BY prop1 DESC, prop2 ASC so I have this result:

[{
    "name": "Object 2",
    "prop1": 6,
    "prop2": 4
}, {
    "name": "Object 1",
    "prop1": 5,
    "prop2": 2
}, {
    "name": "Object 3",
    "prop1": 5,
    "prop2": 3
}]

How can I sort an array a) descending by a key and b) by multiple keys?

Version: jq 1.5