Home > Enterprise >  Order List By Date, Id and Parent Id (Guid)
Order List By Date, Id and Parent Id (Guid)

Time:06-28

I have a List with ID, Parent_ID and Date and I want to order it in C# with linq an OrderBy()

ID Parent_ID Date
Guid1 null 13.05.22
Guid2 null 16.05.22
Guid3 Guid1 17.05.22
Guid4 Guid1 18.05.22
Guid5 Guid2 20.05.22

Now I need to order the List. The current Date should be at top but I need to group it by the ID and Parent_ID. The List should look like following.

ID Parent_ID Date
Guid2 null 16.05.22
Guid5 Guid2 20.05.22
Guid1 null 13.05.22
Guid3 Guid1 17.05.22
Guid4 Guid1 18.05.22

EDIT

This is my code

List<MyModel> items = myList
                    .Select(s => new MyModel
                    {
                        Id = s.Id,
                        ParentId = s.ParentId,
                        Date = s.Date
                    })
                    .OrderByDescending(o => o.Date)
                    .ThenBy(o => o.Parent_Id == null)
                    .ThenBy(o => o.Parent_Id)
                    .ToList();

MyModel

public class MyModel
    {
        public Guid Id { get; set; }

        public Guid? ParentId { get; set; }

        public DateTime Date { get; set; }
}

Some Values

var myList = new List<MyModel>
{
    new MyModel {Id = Guid.Parse("EDDC393F2D304C97B7BA744AE75A13B1"), ParentId = null, Date = new DateTime(2007, 12, 10) },
            new MyModel {Id = Guid.Parse("99F31A6397C04A5A9334275660F2614D"), ParentId = null, Date = new DateTime(2018, 01, 10) },
            new MyModel {Id = Guid.Parse("19F31A6397C04A5A9334275660F2614B"), ParentId = null, Date = new DateTime(2022, 05, 13) },
            new MyModel {Id = Guid.Parse("10BB917E50344DAD8D09FA2F3ACDFD12"), ParentId = null, Date = new DateTime(2022, 05, 16) },
            new MyModel {Id = Guid.NewGuid(), ParentId = Guid.Parse("19F31A6397C04A5A9334275660F2614B"), Date = new DateTime(2022, 05, 17) },
            new MyModel {Id = Guid.NewGuid(), ParentId = Guid.Parse("19F31A6397C04A5A9334275660F2614B"), Date = new DateTime(2022, 05, 18) },
            new MyModel {Id = Guid.NewGuid(), ParentId = Guid.Parse("10BB917E50344DAD8D09FA2F3ACDFD12"), Date = new DateTime(2022, 05, 20) },
            new MyModel {Id = Guid.Parse("A9F31A6397C04A5A9334275660F2614C"), ParentId = null, Date = new DateTime(2019, 05, 20) },
            new MyModel {Id = Guid.Parse("00000233010247B99282C151931FF6C6"), ParentId = null, Date = new DateTime(2011, 06, 05) },   
};

CodePudding user response:

This will order first by Date and then by Parent ID.

myList = myList.OrderByDescending(x => x.Date).ThenBy(x=>x.ParentId).ToList();

Here are the Results

enter image description here

Update

Code according to enter image description here

CodePudding user response:

I tyred the following code. But it is really slow if the Data amount is big.

var newList = new List<MyModel>();

            foreach (var item in myList.Where(w => w.ParentId == null).OrderByDescending(o => o.Date))
            {
                newList.Add(item);

                var subList = myList.Where(w => w.ParentId == item.Id).OrderByDescending(o => o.Date);

                foreach (var sub in subList)
                {
                    newList.Add(sub);
                }
            } 
  • Related