如何通过依赖依赖对象进行排序 [英] How to sort depended objects by dependency
问题描述
我有一个集合:
名单,其中,VPair<项目,列表和LT;项目>> dependencyHierarchy;
在一对中的第一项是一些对象(项目),第二个是同一类型的对象的集合,所述第一一个取决于。我希望得到一个名单,其中;项目>
为了依赖,所以不会有依赖的第一个元素等项目(无循环依赖!)。 P>
输入:
ITEM4取决于项目3和Item5 项目3取决于项目1 项目1不依赖于任何一个 项目2取决于ITEM4 Item5不依赖于任何一个
结果:
项目1 Item5 项目3 ITEM4 项目2
感谢你。
解决方法:
拓扑排序(感谢卢瓦克FEVRIER 的想法)
和
例如在C#,的example关于Java (感谢的 xcud 获得很好的例子)
完美的例子使用拓扑排序:
<一个href="http://en.wikipedia.org/wiki/Topological_sorting">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.
这篇关于如何通过依赖依赖对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!