Google sheets Script – Sort sheet by column AP under category headers (colored background)

I am fairly new to Javascript/Apps Script.

I have a sheet that contains a few categories and their data on a single sheet. I am trying to create a script that sorts whats under each category (header) by column AP.

Currently we are manually selecting the range then running the following macro to sort by the column.

function SortAP() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getActiveRange()
  .sort({column: 42, ascending: true});
};

I want to have this query a row for a header and then run the function. Is this possible?

Here is an example sheet of what our headers look like