如何按关系 child/parent 的 2 个字段排序。对其进行排序,以便子项在排序列表中跟随其父项?

How to order by 2 fields with relation child/parent. Sort it so that child items follow their parents in the sorted list?

我在 table 查询如下。

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;
                    }
                }