# TakeLargestBy nested variable in Dataset

I would like to TakeLargestBy a nested variable in a Dataset. To be clearer, here is code to make an example dataset

nObs = 4;
mkPartAssoc =
Association[
RandomInteger[{4, 9}, 3]]]] &;
data = Dataset@
Reap[Do[Sow[mkPartAssoc[]], nObs]][[2, 1]]]]}]


Sample result

I can take the largest 1 observation by the non-nested variable as follows: data[TakeLargestBy["ID", 1]],

But how can I take the largest 1 observation by "part1"? My attempts below do not work: data[TakeLargestBy["ID"["part1"], 1]]

data[TakeLargestBy["part1", 1]]

One way is to use a pure function that extracts the desired subvalue from each association:

data[TakeLargestBy[#["thing", "part1"] &, 1]]


Part syntax could be used instead:

data[TakeLargestBy[#[["thing", "part1"]] &, 1]]


For nested associations as in the question, this is equivalent to the extraction syntax above. But it is more general in that it can be used to index into hierarchies whose levels are arbitrary mixtures of associations and lists.

The desired subparts could also by accessed by means of a subquery:

data[TakeLargestBy[Query["thing", "part1"], 1]]