我有一個類似于下面的串列。我想根據我指定的規則對這個串列進行排序。如果有一個元素附加了串列元素,它必須在前排。
例如,由于 Id 為 2 的記錄的 RequiredCourse 欄位為 3,因此 Id 為 3 的元素必須位于 Id 為 2 的元素之前。為此,我創建了一個交換方法,如下所示。但是 Id 3 的記錄取決于 4,我無法正確創建排序。此外,它取決于 ID 為 8 及以下的 10。但是 9 沒有連接到任何東西。由于我更改了 ID 為 8 和 10 的記錄的位置,因此 8 位于 9 之后。
示例輸出應如下所示:
var list = new List<Course>();
list.Add(new Course { Id = 1 });
list.Add(new Course { Id = 2, RequiredCourse = "3" });
list.Add(new Course { Id = 3, RequiredCourse = "4" });
list.Add(new Course { Id = 4 });
list.Add(new Course { Id = 5 });
list.Add(new Course { Id = 6 });
list.Add(new Course { Id = 7 });
list.Add(new Course { Id = 8, RequiredCourse = "10" });
list.Add(new Course { Id = 9 });
list.Add(new Course { Id = 10 });
list = list.OrderBy(i => i.Id).ThenBy(i => i.RequiredCourse).ToList();
var copy = new List<Course>(list);
for (int i = 0; i < copy.Count; i )
{
if (!string.IsNullOrEmpty(copy[i].RequiredCourse) && Int32.Parse(copy[i].RequiredCourse) > copy[i].Id)
{
var index = list.FindIndex(k => k.Id == Int32.Parse(copy[i].RequiredCourse));
if (index > -1)
{
var temp = list[i];
list[i] = list[index];
list[index] = temp;
}
}
}
使用上面的代碼有缺陷的輸出
1
3 4
4
2 3
5
6
7
10
9
8 10
預期產出
1
4
3 4
2 3
5
6
7
10
8 10
9
uj5u.com熱心網友回復:
問題是您想要“4 - ''”下方的專案“2 - 3”。我確信有一種方法可以改進我所做的事情,但這是 1.0 版:
var list = new List<Course>();
list.Add(new Course { Id = 1 });
list.Add(new Course { Id = 2, RequiredCourse = "3" });
list.Add(new Course { Id = 3, RequiredCourse = "4" });
list.Add(new Course { Id = 4 });
list.Add(new Course { Id = 5 });
list.Add(new Course { Id = 6 });
list.Add(new Course { Id = 7 });
list.Add(new Course { Id = 8, RequiredCourse = "10" });
list.Add(new Course { Id = 9 });
list.Add(new Course { Id = 10 });
// isolating items with RequiredCourse
var withRequired = (
from o in list
where !string.IsNullOrWhiteSpace(o.RequiredCourse)
select o
).ToList();
list = list.Except(withRequired).ToList();
// First I arrange the items without "RequiredCourse" with their respective ones.
for (var i = 0; i < list.Count; i )
{
var primary = list[i];
var parents =
withRequired
.Where(o => o.RequiredCourse == primary.Id.ToString())
.OrderBy(o => o.Id)
.ToList();
var children = (
from o in withRequired.Except(parents)
join o2 in parents
on o.Id.ToString() equals o2.RequiredCourse
into grp
from o3 in grp
orderby o3.Id descending
select o3
).ToList();
if (!parents.Any())
{
continue;
}
i ;
foreach (var p in parents)
{
list.Insert(i , p);
foreach (var c in children.Where(o => p.Id.ToString() == o.RequiredCourse))
{
list.Insert(i , c);
}
}
}
// retrieving items that only link to items that have RequiredCourse
var childOfChildren = withRequired.Except(list).ToList();
// Then I arrange items with RequiredCourses that are linked to other items with RequiredCourses.
for (var i = 0; i < list.Count; i )
{
var primary = list[i];
var children = (
from o in childOfChildren
where primary.Id.ToString() == o.RequiredCourse
orderby o.Id descending
select o
).ToList();
if (!children.Any())
{
continue;
}
i ;
list.InsertRange(i, children);
}
// CONSOLE APPLICATION PART
foreach (var o in list)
{
if (string.IsNullOrEmpty(o.RequiredCourse))
{
o.RequiredCourse = "-";
}
Console.WriteLine($"Course: {o.Id} - {o.RequiredCourse}");
}
uj5u.com熱心網友回復:
我認為這很容易理解。
var list = new List<Course>();
list.Add(new Course { Id = 1 });
list.Add(new Course { Id = 2, RequiredCourse = "3" });
list.Add(new Course { Id = 3, RequiredCourse = "4" });
list.Add(new Course { Id = 4 });
list.Add(new Course { Id = 5 });
list.Add(new Course { Id = 6 });
list.Add(new Course { Id = 7 });
list.Add(new Course { Id = 8, RequiredCourse = "10" });
list.Add(new Course { Id = 9 });
list.Add(new Course { Id = 10 });
var output = new List<Course>();
var toProcess = new SortedDictionary<int, Course>(list.ToDictionary(c => c.Id));
var pendingAdd = new Stack<Course>();
while (toProcess.Count > 0)
{
Course currentCourse = toProcess.First().Value;
if (string.IsNullOrEmpty(currentCourse.RequiredCourse))
{
// Course has no dependency, process it.
output.Add(currentCourse);
toProcess.Remove(currentCourse.Id);
}
else
{
int courseId = currentCourse.Id;
// Course has dependency. Trace down linked courses.
while (toProcess.ContainsKey(courseId) && !string.IsNullOrEmpty(toProcess[courseId].RequiredCourse))
{
pendingAdd.Push(toProcess[courseId]);
courseId = int.Parse(toProcess[courseId].RequiredCourse);
}
// dont forget to add the "top-level" course for the dependency chain
pendingAdd.Push(toProcess[courseId]);
// Add in reverse depdency order using Stack
while (pendingAdd.Count > 0)
{
var course = pendingAdd.Pop();
output.Add(course);
toProcess.Remove(course.Id);
}
}
}
您的通緝名單在output.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/357893.html
