How to sort depended objects by dependency(如何按依赖对依赖的对象进行排序)
问题描述
我有一个收藏:
列表<VPair<Item, List<Item>>依赖层次结构;
对中的第一个项目是某个对象(项目),第二个是第一个所依赖的相同类型对象的集合.我想按依赖顺序获取
List<Item>
,因此没有项目依赖于第一个元素等等(没有循环依赖!).输入:
<上一页>Item4 依赖于 Item3 和 Item5Item3 依赖于 Item1Item1 不依赖于任何一个Item2 依赖于 Item4Item5不依赖任何一个结果:
<上一页>项目1项目5第 3 项项目4项目2
谢谢.
解决方案:
拓扑排序(感谢 Loïc Février 的想法)
和
C# 示例, Java 示例(感谢 xcud 示例)
使用拓扑排序的完美示例:
http://en.wikipedia.org/wiki/Topological_sorting
它会为您提供您所需要的.
I have a collection:
List<VPair<Item, List<Item>> dependencyHierarchy;
The first item in pair is some object (item) and the second one is a collection of the same type objects that the first one depends on. I want to get a List<Item>
in order of dependency, so there are not items that depend on the first element and so on (no cycled dependency!).
Input:
Item4 depends on Item3 and Item5 Item3 depends on Item1 Item1 does not depend on any one Item2 depends on Item4 Item5 does not depend on any one
Result:
Item1 Item5 Item3 Item4 Item2
Thank you.
SOLUTION:
Topological Sorting (thanks to Loïc Février for idea)
and
example on C#, example on Java (thanks to xcud for great examples)
Perfect example to use a topological sort :
http://en.wikipedia.org/wiki/Topological_sorting
It will give you exactly what you need.
这篇关于如何按依赖对依赖的对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何按依赖对依赖的对象进行排序


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01