У меня есть структура данных, набранная так:
class Something {
public string Name { get; set; }
public List<Something> Children {get; set;}
public Something() {
Children = new List<Something>();
}
}
Пример данных:
var one = new Something();
one.Name = "B";
var two = new Something();
two.Name = "A";
var three = new Something();
three.Name = "C";
one.Children.Add(new Something { Name = "F"});
one.Children.Add(new Something { Name = "E"});
one.Children.Add(new Something { Name = "D"});
three.Children.Add(new Something { Name = "F"});
three.Children.Add(new Something { Name = "E"});
three.Children.Add(new Something { Name = "D"});
var data = new List<Something>();
data.Add(one);
data.Add(two);
data.Add(three);
Мне нужна функция для сортировки поля на все уровни , глубина дерева равна произвольно .
Пока у меня есть это:
public static List<Something> SortTree(List<Something> node) {
if (node == null) {
return null;
}
return node
.OrderBy(x => x.Name)
.Select(y => {
if (y.Children.Count() > 0) {
var t = y.Children;
SortTree(t.ToList());
}
return y;
})
.ToList();
}
Вызов SortTree(data)
возвращает данные только сРодительский уровень отсортирован.
Спасибо за помощь.