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

推荐订阅源

Cloudbric
Cloudbric
有赞技术团队
有赞技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Threat Research - Cisco Blogs
L
LangChain Blog
Simon Willison's Weblog
Simon Willison's Weblog
Project Zero
Project Zero
Latest news
Latest news
S
Schneier on Security
Cisco Talos Blog
Cisco Talos Blog
MyScale Blog
MyScale Blog
C
Check Point Blog
IT之家
IT之家
P
Palo Alto Networks Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
G
Google Developers Blog
T
Tor Project blog
T
Threatpost
D
DataBreaches.Net
博客园 - 【当耐特】
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Troy Hunt's Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
P
Privacy & Cybersecurity Law Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cisco Blogs
博客园_首页
S
Securelist
T
The Exploit Database - CXSecurity.com
Last Week in AI
Last Week in AI
量子位
U
Unit 42
Know Your Adversary
Know Your Adversary
Hugging Face - Blog
Hugging Face - Blog
S
Security Affairs
Google Online Security Blog
Google Online Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志

博客园 - 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);
            }
        }