How to sort Repeating Group by each items Max/Min?

Hi there!
I’m trying to allow users to sort my repeating group by each of the column headings however am having trouble sorting using min/max/average values of each brand’s car servicing costs.


I have been using different states to define the ‘data source’ to use. Technically it is always the same data source, I just change the ‘sorting’ in each data source to give the sorting we want e.g. state will be a number 1-8 corresponding to:
1 = Brand (Ascending)
2 = Brand (Descending)
3 = Min Cost (Ascending)
4 = Min Cost (Descending)
5 = Avg Cost (Ascending)
6 = Avg Cost (Descending)
7 = Max Cost (Ascending)
8 = Max Cost (Descending)

This design has worked for sorting Brands alphabetically - but it I’m having trouble sorting values given the values in the regeating group are the min/max/average service cost of each car’s costs grouped by brand.

Sorting by values is typically easy, the only difference here is that I’m seeking to sort by things that are ‘grouped by’ . I have tried sorting by a ‘dynamic field’ with no success. Does anyone know what query would achieve my intended result of sorting by max/min service cost?

Open to ideas if anyone has a better way of achieving the outcome of being able to sort by each column.