我在表格中查询如下。
Id
BeforeId
Description
例如:
Id BeforeId Description
1 NULL test
2 NULL test1
3 2 test2
4 3 test3
如果 BeforeId 不为 null,则将在 Id 之前推送。我想按 Id 和 BeforeId 排序,按以下顺序排序。
Id BeforeId Description
1 NULL test
4 3 test3
3 2 test2
2 NULL test1
我正在尝试如下代码,但事实并非如此。
var listOrder = _entites.Orders.OrderBy(t => t, new CustomComparer()).ToList();
public class CustomComparer : IComparer<Order>
{
public int Compare(Order lotA, Order lotB)
{
if (lotA.BeforeId!=null)
{
if (lotB.Id == lotA.BeforeId)
{
return -1;
}
else
{
return 0;
}
}
else if(lotB.BeforeId != null )
{
if(lotA.Id == lotB.BeforeId)
{
return 1; // A > B
}
else
{
return 0;
}
}
else
{
return 0;
}
}
}
谁能告诉我如何解决这个问题。
谢谢!
第一个我创建了一个视图模型(添加 Seq 列(并像这样排序
Id BeforeId Description Seq
1 NULL test 1
2 NULL test1 2
3 2 test2 3
4 3 test3 4
我将自动生成序列号。之后,我将通过查找每个元素在id之前再次排序列表来更新seq。大数据可能需要很多时间。
//list order need to sort
var listNeedToSort = _entites.Order.ToList();
//list order have before id
var listBeforeId = listNeedToSort.Where(p=>p.BeforeId!=null).Select(p => p.BeforeId).ToList();
//count number of duplicate data is not process
int countLoopDuplicateButNotProcess = 0;
while (listBeforeId.Any())
{
foreach (var item in listNeedToSort.OrderByDescending(p => p.BeforeId))
{
if (item.BeforeId != null)
{
//get record which is mentioned by other record through beforeid.
var recordSummary = listNeedToSort.FirstOrDefault(p => p.Id == item.BeforeId);
if (recordSummary != null)
{
// if sequence number of item with before id greater than record which has id equals beforeid
if (item.Seq > recordSummary.Seq)
{
//reset count loop but it process again
countLoopDuplicateButNotProcess = 0;
item.Seq = recordSummary.Seq;
//sort again list
foreach (var item1 in listNeedToSort.Where(p => p.Seq >= recordSummary.Seq && p.Id != item.Id).OrderBy(p => p.Seq))
{
item1.Seq += 1;
}
//remove beforeid in listBeforeId
listBeforeId.Remove(item.BeforeId);
}
else
{
//not process
countLoopDuplicateButNotProcess += 1;
}
}
else
{
//reset count loop but it process again
countLoopDuplicateButNotProcess = 0;
//remove beforeid in listBeforeId
listBeforeId.Remove(item.BeforeId);
}
}
else
{
//not process
countLoopDuplicateButNotProcess += 1;
}
}
//break if not process two times.
if (countLoopDuplicateButNotProcess == 2)
{
break;
}
}