惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
I
InfoQ
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
有赞技术团队
有赞技术团队
G
Google Developers Blog
L
LangChain Blog
博客园_首页
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
月光博客
月光博客
IT之家
IT之家
量子位
宝玉的分享
宝玉的分享
S
SegmentFault 最新的问题
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网

博客园 - vuejs3

More on SQL Server Service Broker [转]Android试验:如果View的ID相同会出现什么效果? Silverlight 操作技巧 [文摘]怎么使用Sticky Footer代码(让页脚紧贴页面底部的方法) Sharepoint development toolbox 通过对象模型上传List Template - vuejs3 - 博客园 在文档库中隐藏多文件上传/Disable the Upload Multiple Document option in Document Library - vuejs3 [转]SharePoint Document Library and List – File Upload ASP.NET 2.0 TreeView控件在IE7中断开的连接线 Team Foundation Server讲义 通过CertEnroll在CA上(1创建证书请求2得到证书3安装证书) 如何在WSS中利用KeywordQuery创建搜索查询 SharePoint Web Service的身份验证 SharePoint开发中对ListViewWebPart的几个操作 【转】免费SharePoint资源 Enum操作技巧 LINQ概述-通用和便利的信息查询方式 NetShopForge网上商店程序(VB)源码—讨论-发布 Microsoft AJAX添加自定义智能感知(intellisense)
父子关系排序,无确定根节点
vuejs3 · 2010-03-15 · via 博客园 - vuejs3

需要更新一批数据,需要按照父子关系依次更新,前提是如果有父节点一定要先更新父节点。

这个算法感觉有些麻烦,不知是否有更好的方法

示例数据:

ID,ParentID
(1, -1),(5, -1),(323, 32), (555, 55),(55, 5),(87, 8),(633, 63),(63, 6),(2, -1),(3232, 323)

排序后的结果
  1,5,55,555,323,3232,87,63,633,2

var list = new List<KeyValuePair<int,int>>
               {
                   
new KeyValuePair<intint>(1-1),
                   
new KeyValuePair<intint>(5-1),
                   
new KeyValuePair<intint>(32332),
                   
new KeyValuePair<intint>(55555),
                   
new KeyValuePair<intint>(555),
                   
new KeyValuePair<intint>(878),
                   
new KeyValuePair<intint>(63363),
                   
new KeyValuePair<intint>(636),
                   
new KeyValuePair<intint>(2-1),
                   
new KeyValuePair<intint>(3232323)
               };
var testList 
= GetSortedList(list);

 排序

        private static List<KeyValuePair<intint>> GetSortedList(IEnumerable<KeyValuePair<intint>> list)
        {
            var tempList 
= new List<KeyValuePair<intint>>();
            tempList.AddRange(list);
            var result 
= new List<KeyValuePair<intint>>();
            
foreach (var keyValuePair in list)
            {
                
if (!tempList.Contains(keyValuePair)) continue;
                var parentList 
= new List<KeyValuePair<intint>>();
                BuildParent(keyValuePair.Value, 
ref parentList, list);
                
for (var i = parentList.Count - 1; i >= 0; i--)
                {
                    
if (!tempList.Contains(parentList[i])) continue;
                    result.Add(parentList[i]);
                    tempList.Remove(parentList[i]);
                }
                result.Add(keyValuePair);
                tempList.Remove(keyValuePair);

                var childList 

= new List<KeyValuePair<intint>>();
                BuildChild(keyValuePair.Key, 
ref childList, list);
                
for (var i = 0; i < childList.Count; i++)
                {
                    
if (!tempList.Contains(childList[i])) continue;
                    result.Add(childList[i]);
                    tempList.Remove(childList[i]);
                }
            }
            
return result;
        }

构建孩子列表和父亲列表

        private static void BuildChild(int parentId, ref List<KeyValuePair<intint>> newlist,
                                                      IEnumerable
<KeyValuePair<intint>> list)
        {
            var children 
= from keyValuePair in list
                           
where keyValuePair.Value == parentId
                           select keyValuePair;
            
foreach (var child in children)
            {
                newlist.Add(child);
                BuildChild(child.Key, 
ref newlist, list);
            }
        }
        
private static void BuildParent(int childId, ref List<KeyValuePair<intint>> newlist,
                                                      IEnumerable
<KeyValuePair<intint>> list)
        {
            
           var parents 
= from keyValuePair in list
                                  
where keyValuePair.Key == childId
                                   select keyValuePair;
             
foreach (var parent in parents)
                {
                 newlist.Add(parent);
                 BuildParent(parent.Value, 
ref newlist, list);  
                }
 
          
        }

 如果有固定的根节点就好排序多了

            var list = new List<KeyValuePair<int,int>>
                           {
                               
new KeyValuePair<intint>(1-1),
                               
new KeyValuePair<intint>(5-1),
                      
        new KeyValuePair<intint>(555),
                               
new KeyValuePair<intint>(17, 1),
                               
new KeyValuePair<intint>(558, 55),
                               
new KeyValuePair<intint>(174, 17),
                               
new KeyValuePair<intint>(2-1),
                          
                           };
            var result
= new List<KeyValuePair<intint>>();
            BuildParentChildList(
-1ref result, list);

     private static  void BuildParentChildList(int parentId,ref List<KeyValuePair<intint>> targetList ,
                                                           List
<KeyValuePair<intint>> sourceList)
        {
            var dic 
= from source in sourceList
                           
where  source.Key== parentId
                           select source;
            
foreach (var source in dic)
            {
                var childParentId 
= source.Key;
                targetList.Add(source);
                BuildParentChildList(childParentId, 
ref targetList, sourceList);
            }
        }