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.
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.