Search code examples
google-sheetsgoogle-sheets-formula

Sort output of index based on values not in array


I have a Google Sheet where each column represents a different topic, and every column contains 5 groups of 5 rows outlining the topic details along with an assigned name and status.

enter image description here

Thanks to @rockinfreakshow's help on this thread, I am using the following formula to search each column from top to bottom and display the topic and its details for the first 5 matches of a specified name if the status is not 'Complete':

=INDEX(SPLIT(ARRAY_CONSTRAIN(TOCOL(IF(($C5:$I21=B32)*($C6:$I22<>"Complete"),$C2:$I2&"|"&$C3:$I19,),1,1),5,1),"|"))

enter image description here

What I am trying to figure out now, however, is how to sort this data by the priority levels in rows 4, 8, 12, 16, and 20. (If there is no priority level, the data should appear in the order of the columns from top to bottom, left to right, after the data with priority levels.) This is the order in which I want the data to be output:

enter image description here

I have tried using SORT but I am not sure how to point it toward the priority data, as this seems to be sorting by the data in the first row of the array:

=INDEX(SORT(SPLIT(ARRAY_CONSTRAIN(TOCOL(IF(($C5:$I21=B32)*($C6:$I22<>"Complete"),$C2:$I2&"|"&$C3:$I19,),1,1),5,1),"|"),3,1))

How would I best accomplish what I am looking for?


Solution

  • You can sort once you include the Priority rows (&"|"&$C4:$I20) as well in the existing formula & in the final step you can exclude this new\extra column using choosecols():

    =choosecols(sortn(split(tocol(if(($C5:$I21=B32)*($C6:$I22<>"Complete"),$C2:$I2&"|"&$C3:$I19&"|"&$C4:$I20,),1,1),"|"),5,,3,1),1,2)
    

    enter image description here