StefanoM-9408 avatar image
0 Votes"
StefanoM-9408 asked LeonLu-MSFT commented

Sort items within groups of a CollectionView in chronological order

I have an ObservableCollection in which I create groups and insert elements within each group. When you associate yours with a CollectionView, I view the groups in chronological order by Date, while the elements inside are in chronological but increasing order, from the oldest to the most recent and I would like to invert them. I have tried various ways, but without success

 foreach (MyElement hd in MyList.Reverse())
     MyGroup  group = new MyGroup("Name", new ObservableCollection<MyElement>());
          if (!MyCollection.Contains(group))

Using MyList.Reverse () in the foreach loop, start inserting the elements from the oldest to the most recent, and in fact the groups are sorted like this, while the elements inside it are backwards

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented


Welcome to our Microsoft Q&A platform!

You can use System.Linq to achieve the sort by chronological order with List<Contact> newSort= Contacts.OrderByDescending(x => x.CreateTime).ToList<Contact>();

public ObservableCollection<Contact> Contacts { get; set; }
        public ObservableCollection<Contact> SelectItems { get; set; }
        public MyViewModel()
            Contacts = new ObservableCollection<Contact>();
            SelectItems = new ObservableCollection<Contact>();
            Contacts.Add(new Contact() { Name="contact1", PhoneNumber=12356677, CreateTime=new DateTime(2015,1,6) });
            Contacts.Add(new Contact() { Name = "contact2", PhoneNumber = 12444677, CreateTime = new DateTime(2011, 10, 1) });
            Contacts.Add(new Contact() { Name = "contact3", PhoneNumber = 2366677, CreateTime = new DateTime(2012, 2, 7) });
            Contacts.Add(new Contact() { Name = "contact4", PhoneNumber = 200356677, CreateTime = new DateTime(2014, 12, 6) });
            Contacts.Add(new Contact() { Name = "contact5", PhoneNumber = 1230056677, CreateTime = new DateTime(2021, 1, 6) });
            List<Contact> newSort= Contacts.OrderByDescending(x => x.CreateTime).ToList<Contact>();
            foreach (Contact item in newSort)

My insert sort is disorderly, Here is my running screenshot.


Best Regards,

Leon Lu

If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

image.png (141.0 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

When I put the items in a simple list, they come to me in chronological order, but if I create groups for each month, and inside all the items created that month, they come inside the group all in reverse, from the oldest to the most recent


0 Votes 0 ·

What is type of the Control.ControlDiary? If it is ObservableCollection, you can use Control.ControlDiary.OrderByDescending(x => x.CreateTime).ToList<object>();

0 Votes 0 ·