I know and realise I have been reinventing the wheel here a bit and there is redundant code here. Also, I know already there are better ways of doing things. But I thought I'd spent a bit of time and some of it might still be useful at some point.
I guess we could find dependent tables in the sort function I wrote somehow and then join on
not using this as it was only for communication and possible ideas