More generalized code for this requirement is below.
var subheaders = [ [ [1,4,7], 'alpha', 'yellow'], [ [2,6,9], 'beta', 'green'], [ [3,5,8,10], 'theta', 'red'] ]; //Need to add row values and sub header to be displayed
var list = $('input[name=hid_row_list_[% QuestionName() %]]').val().split(',').map(Number); //mapping / listing the row values in the question
subheaders.forEach(function(subheader){ //we need to iterate this function for array subheader times
var earliestIndex = 9999; //default value of eariest index
for(var i=0; i<subheader[0].length;i++)//we need to iterate a for loop for array in subheaders array
{
var thisIndex = list.indexOf(subheader[0][i]); //checking if the row elements exists in the list order
console.log(thisIndex);
if (thisIndex != -1 && thisIndex < earliestIndex) { earliestIndex = thisIndex; } //thisIndex will check the position in list if it is present and < earliest index it will store the earliestindex value as thisindex. now the earliestindex value is thisindex not 9999 again it will check all the given values in subheaders
}
$('#[% QuestionName() %]_r' + list[earliestIndex] +"_row").before("<tr bgcolor='"+subheader[2]+"'><td colspan='8'><b>"+subheader[1]+"</b></td></tr>");//before th earliestindex row we are inserting subheader
});
$(".row_label_cell").css("padding-left","20px");//left aligning the row options to differentiate with sub headers