+ Start a Discussion
Chuck H.Chuck H. 

custom heirachal sorting

I'm trying to sort a list of sObjects based upon several varying 'soft' relationships. I butchered O^n already by traversing creating nested for loops and soon discovered that just wasn't going to work. I am a newer developer so hoping I got all my terms correct and that this isn't something painfully simple that is documented elsewhere.

In starting over here, I'm not too sure where to begin. Here's what I'm trying to achieve...
I have a list of sObjects that I'm trying trying to sort by varying relationships. The ultimate goal is to have these in a linear list sorted by heirachy and also 'family sets'. The data will ultimatey be displaying on a custom Lightning page in various components. I also have varying amounts of some of the RecordTypes, where a parent can have one or many children, sometimes no immediate children (eg - RT's 1, 5, 6* only). The end order should look like this, I've colored/simplified the relationships in an effort to digest this more easily myself...it's not helping much, lol:

OCR friendly table of data and relationships. Message with any questions!

I've done some basic comparable interfaces for sorting in the past, but doesn't seem like a fit for this case. Are there suggestions on efficient ways to sort the records above when given in a random order? Reach out with any questions and I'll be happy to elaborate. To be clear, I can be given multiple families at once and they can contain any number of each RecordType. It is assumed that all colored relationships will be in place and unique to each parent.

For reference, here's the approach I originally tried. This 'works' for smaller sets with only one familay but doesn't get me much past that...I am using a wrapper class, hence the .variableNames without __c's. I know enough to know this is horribly inefficient!
for(TreeRow product : prodSvRows){
            sortedList.add(product);
            for(TreeRow exp : expRows){
                if(exp.parentRowId == product.serviceRowSpec){
                    sortedList.add(exp);    
                }
                for(TreeRow pattern : ptnRows){
                    if(pattern.parentRowId == product.rowId){
                        sortedList.add(pattern);
                    }
                    for(TreeRow stage : stageRows){
                        if(stage.parentRowId == pattern.rowId){
                            sortedList.add(stage);
                        }
                    }
                }
                for(TreeRow cadSpec : cadSpRows){
                    if(cadSpec.parentRowId == exp.rowId){
                        sortedList.add(cadSpec);
                    }
                    for(TreeRow cadSvc : cadSvRows){
                        if(cadSvc.rowMdmKey == cadSpec.rowLabel){
                            sortedList.add(cadSvc);
                        }
                    }
                }
           } 
     }

Yaay, first question posted!