{"id":7879,"date":"2023-06-20T15:40:35","date_gmt":"2023-06-20T14:40:35","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=7879"},"modified":"2023-06-20T15:40:37","modified_gmt":"2023-06-20T14:40:37","slug":"c-tree-sorting-for-inserting-rows-in-the-correct-order","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-tree-sorting-for-inserting-rows-in-the-correct-order\/","title":{"rendered":"C# Tree sorting for inserting rows in the correct order"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">public interface ITreeNode\n{\n\tGuid Id { get; }\n\tGuid? ParentId { get; }\n}\n\npublic class TreeSorter\n{\n\tpublic static IEnumerable&lt;T> Sort&lt;T>(IEnumerable&lt;T> allNodes) where T: ITreeNode\n\t{\n\t\tvar sortedNodes = new List&lt;T>();\n\t\tvar visited = new HashSet&lt;Guid>();\n\t\tforeach (var node in allNodes)\n\t\t\tVisit(allNodes, node, visited, ref sortedNodes);\n\t\treturn sortedNodes;\n\t}\n\n\tprivate static void Visit&lt;T>(IEnumerable&lt;T> allNodes, T node, HashSet&lt;Guid> visited, ref List&lt;T> sortedNodes) where T : ITreeNode\n\t{\n\t\tif (visited.Contains(node.Id)) return;\n\t\tvisited.Add(node.Id);\n\t\tvar parentNode = allNodes.FirstOrDefault(n => n.Id == node.ParentId);\n\t\tif (parentNode != null)\n\t\t\tVisit(allNodes, parentNode, visited, ref sortedNodes);\n\t\tsortedNodes.Add(node);\n\t}\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7879","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/7879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=7879"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/7879\/revisions"}],"predecessor-version":[{"id":7880,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/7879\/revisions\/7880"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=7879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=7879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=7879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}