'Sort DataTable with self referential Relationship

I have a requirement where I need to sort out a data table with self referential relationship.

My Table looks something below as shown in the image. I want all the related subset records should be displayed under the parent record. Any Help?

Existing Table

|:------------------------------------------------------:| |Id....................|Color...........|.IsSubset?.....|..SubsetOf...|

|:------------------------------------------------------:|

|11111111.....|..Black..........| No................|...........................|

|234435..........|..Blue............| No.............. | ...........................|

|43657668.... |..Red............ | yes............. | 1111111111. |

|5767876....... |..Green....... | yes............. | 1111111111. |

|65876987.... | yellow........ | yes............. | 234435.......... |

|7697800....... | orange...... | yes............. | 234435.......... |

|:------------------------------------------------------:|

Expected table

|:------------------------------------------------------:| |Id....................|Color...........|.IsSubset?.....|..SubsetOf...|

|:------------------------------------------------------:|

|11111111.....|..Black..........| No................|...........................|

|43657668.... |..Red............ | yes............. | 1111111111. |

|5767876....... |..Green....... | yes............. | 1111111111. |

|234435..........|..Blue............| No.............. | ...........................|

|65876987.... | yellow........ | yes............. | 234435.......... |

|7697800....... | orange...... | yes............. | 234435.......... |

|:------------------------------------------------------:|

Tables



Solution 1:[1]

Grab all the rows that have no parent and order them. Then loop thorough each of those, recursively calling the same logic (passing in the row you are on), but this time order (and recursively call) the rows where the parent is the id from passed in row.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Beakie