Sort items within groups of a CollectionView in chronological order

Stefano M 91 Reputation points
2021-03-24T21:08:47.277+00:00

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))
           {
                  MyCollection.Add(group);
            }
        group.Add(hd);
}

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2021-03-25T05:42:45.63+00:00

    Hello,​

    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>();  
                   Contacts.Clear();  
                   foreach (Contact item in newSort)  
                   {  
                       Contacts.Add(item);  
                   }  
    

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

    81385-image.png

    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.

    1 person found this answer helpful.