c# - 按字符串数组对列表进行排序c#
问题描述
我正在尝试按列表中的其他值对列表进行排序。
当我试图完成时:当我向列表添加一个值时,我将使用一个包含该值的方法,以及一个表示它的依赖项的值的字符串 []。
我想通过 ListObject 中的 string[] 中的值对列表进行排序
这是一个程序示例:
public class ListObject {
private string name;
private string[] dependencies;
private object value;
public ListObject(string name, string[] dependencies, object value) {
this.name = name;
this.dependencies = dependencies;
this.value = value;
}
}
public class ExampleClass {
public static void Main(string[] args) {
List<ListObject> list = new List<ListObject>();
list.Add(new ListObject("ran", new string[] { }, "Value"));
list.Add(new ListObject("far", new string[] {"thest"}, "Value"));
list.Add(new ListObject("the", new string[] {"ran"}, "Value"));
list.Add(new ListObject("thest", new string[] {"the", "ran"}, "Value"));
list.Add(new ListObject("man", new string[] {"ran", "thest"}, "Value"));
//What I want the order of the list to become
/* ran
* the
* far
* thest
* man
*/
}
}
更多信息:我在运行时生成类,我需要确保如果一个字段具有生成的类类型,那么我需要确保在生成该字段所在的类之前生成该字段所依赖的类。
解决方案
Found the solution to my issue at: http://tawani.blogspot.com/2009/02/topological-sorting-and-cyclic.html
it's known as topological sorting.