Sort keys based on a particular element in their values

Viewed 70

I'm trying to create a JQ filter that prints the hash of the oldest entry. I'm a beginner and I can't seem to figure it out. A sorted list would suffice because I could pipe to head -1 to get the oldest hash.

My problem is that I can't figure out how to sort based on a value in the array, and then print the key of the array. I can print all the hashes with

jq -r '.t | keys[]'

but it would not be sorted.

The date is in .[26].

This is what I tried:

jq -r '.t[] |= sort_by(.[26]) | keys_unsorted[]'

And this is my input:

    {
      "t": {
        "2C3D7D91DFD0A57CFCA8B7092347B311088D3B6E": [
          "1",
          "0",
          "1",
          "1",
          "Young.Sheldon.S04.1080p.x265-ZMNT",
          "4764367221",
          "1136",
          "1136",
          "4764367221",
          "5201425130",
          "1091",
          "0",
          "0",
          "4194304",
          "",
          "0",
          "0",
          "0",
          "0",
          "0",
          "2",
          "1622459402",
          "0",
          "0",
          "1136",
          "/home/xxxxxxxx/files/Young.Sheldon.S04.1080p.x265-ZMNT",
          "1622421479",
          "2",
          "1",
          "",
          "",
          "4436465131520",
          "1",
          "1"
        ],
        "FAC73275BC376C4C26DFDA41D991D021838DB778": [
          "1",
          "0",
          "1",
          "1",
          "Joshy.2016.NORDIC.1080p.BluRay.REMUX.AVC.DTS-HD.MA.5.1-Danishbits",
          "21160180253",
          "5045",
          "5045",
          "21160180253",
          "4470554624",
          "211",
          "114688",
          "0",
          "4194304",
          "",
          "1",
          "0",
          "1",
          "0",
          "0",
          "2",
          "1622459402",
          "0",
          "0",
          "5045",
          "/home/xxxxxxxx/files/Joshy.2016.NORDIC.1080p.BluRay.REMUX.AVC.DTS-HD.MA.5.1-Danishbits",
          "1622413504",
          "2",
          "1",
          "",
          "",
          "4436465131520",
          "1",
          "1"
        ],
        "671CA27A76DC35E8E9F46723F1F6596A8BC75DA0": [
          "1",
          "0",
          "1",
          "1",
          "Working.Girl.1988.1080p.Bluray.REMUX.AVC.DTS-HD.MA.5.1-4K4U",
          "29680778067",
          "14153",
          "14153",
          "29680778067",
          "12426936320",
          "418",
          "0",
          "0",
          "2097152",
          "",
          "1",
          "0",
          "1",
          "0",
          "0",
          "2",
          "1622459402",
          "0",
          "0",
          "14153",
          "/home/xxxxxxxx/files/Working.Girl.1988.1080p.Bluray.REMUX.AVC.DTS-HD.MA.5.1-4K4U",
          "1622440882",
          "2",
          "1",
          "",
          "",
          "4436465131520",
          "1",
          "1"
        ]
  },
  "cid": 1423760010
}

Here is my desired output:

FAC73275BC376C4C26DFDA41D991D021838DB778
2C3D7D91DFD0A57CFCA8B7092347B311088D3B6E
671CA27A76DC35E8E9F46723F1F6596A8BC75DA0

Or even better just the single

671CA27A76DC35E8E9F46723F1F6596A8BC75DA0

I would really appreciate some advice.

2 Answers

You could form a k/v pair with the name of the root key and the value of .[26] and sort on that. Return the keys from the sorted order

[ 
  .t | 
  (keys_unsorted[]) as $k | 
  { key: $k, value: .[$k][26] } 
] | sort_by(.value)[].key

Or to get the last element alone replace sort in above with sort_by(.value) | last.key

Online demo

Related