My data consists of an array of objects:
x {sortOrder: 4, parentID: -1, questionID: 371}
y {sortOrder: 2, parentID: -1, questionID: 800}
z {sortOrder: 4, parentID: 23, questionID: 123}
p {sortOrder: 1, parentID: -1, questionID: 371}
q {sortOrder: 4, parentID: 371, questionID: 456}
r {sortOrder: 3, parentID: -1, questionID: 371}
s {sortOrder: 2, parentID: 800, questionID: 223}
I want to organize them in the following structure:
Sort Order
Parent ID
- Child (Question ID)
The desired order based on the given example would be:
p y s r x q z
If needed, I am willing to change the "-1" representing a parent to a different value. My goal was to remove it from the sorting process by ensuring it cannot match an actual Question ID.
I have written code that successfully prioritizes the sort order and groups children together, but they are not grouped under their respective parents. All parents are grouped together instead.
return (aSortorder < bSortorder ? -1 : (aSortorder > bSortorder) ? 1 : ((aParentid < bParentid) ? -1 : ((aParentid > bParentid) ? 1 : 0)));
I appreciate any assistance. Thank you.