I am query in table as below.
Id
BeforeId
Description
For Example:
Id BeforeId Description
1 NULL test
2 NULL test1
3 2 test2
4 3 test3
If BeforeId is not null will push before Id. I would like to order by Id and BeforeId with sort in the following order.
Id BeforeId Description
1 NULL test
4 3 test3
3 2 test2
2 NULL test1
I am trying code as below but it is not true.
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;
}
}
}
Can anyone tell me how to do solve this problem.
Thank you!