This is the first cycle - it seems the right rule. I wrote javascript here -
************** LAST CODE ********************** 04/17/2015
http://jsfiddle.net/y74z42ms/25/
For loop 2 - I have to iterate over each element and array rule - and if so, what logic is used to insert a null record?
--- Does it look right?
var input = [ ["AaronG", "RupertJ", "ScottK", "DannyC", "LennyD"], ["JackieP", "RupertJ", "ScottK", "DannyC", "LennyD"], ["NelsaI", "SamJ", "ScottK", "DannyC", "LennyD"], ["BrentD", "SamJ", "ScottK", "DannyC", "LennyD"], ["MaryS", "CardinalE", "DannyC", "LennyD"], ["GaroleP", "CardinalE", "DannyC","LennyD"], ["AlanA", "ChanA", "KopecK", "LennyD"], ["GerryA", "ChanA", "KopecK", "LennyD"], ["BurlS", "TodD", "KopecK", "LennyD"], ["KenS", "TodD", "KopecK", "LennyD"], ["JerryU", "JasonA", "JefferyW", "MargotS", "LennyD"], ["BakerI", "JasonA", "JefferyW", "MargotS", "LennyD"] ]; console.log("input", input); $('#in').html(input.toString()); //:loop1 var ruleStorage = []; var rule = null; var lastRule = null; for (i = 0; i < input.length; i++) { //count the nested array elements - store it if its the longest rule = input[i]; if (lastRule != null) { if (lastRule.length > rule.length) { rule = lastRule; } ruleStorage.push(rule); } lastRule = rule; } ruleStorage = $.unique(ruleStorage); //provides unique rules //:loop1 //:loop2 //use rule 1 console.log("ruleStorage", ruleStorage); rule = ruleStorage[0]; var output = []; for (i = 0; i < input.length; i++) { //count the nested array elements - store it if its the longest var nestedEl = input[i]; var newNest = []; //compare nestedEl with rule - and use the rule to insert null spaces accordingly //create null entries first for (j = 0; j < rule.length; j++) { newNest[j] = null; } //loop through the rule and compare for (j = 0; j < rule.length; j++) { var originalPos = rule.indexOf(rule[j]); var currentPos = nestedEl.indexOf(rule[j]); //build the new nest //if its in the original postion restore it as such if (originalPos == currentPos) { newNest[j] = nestedEl[j]; } //element is new and doesn't exist in the rule if ( currentPos == -1 && originalPos != -1 && rule.indexOf(nestedEl[j]) == -1 && nestedEl[j] !== undefined) { newNest[originalPos] = nestedEl[j]; } //element is not new but its in the wrong place if ( rule.indexOf(nestedEl[j]) != -1 && nestedEl[j] !== undefined) { var rulePos = rule.indexOf(nestedEl[j]); newNest[rulePos] = nestedEl[j]; } } //console.log("newNest", newNest); //console.log("nestedEl", nestedEl); output.push(newNest); } console.log("output", output); $('