I have a ICollectionView which serves as an input source for a WPF ListView. The number of items (text messages) in the CollectionView could be upto 10 thousands. I want to add a sorting creiteria to the collection view based on the TimeStamp. The latest added message should be on top.
MyCollectionView.SortDescriptions.Add(new SortDescription("TimeStamp", ListSortDirection.Descending));
Question: If I use the above sorting criterion, does the sorting takes place every time I add a new message? Or does the CollectionView maintains a sorted list internally and in my scenerio (i.e. having TimeStamp as Sorting), it will only need to compare the new incoming message's TimeStamp with the last added message's TimeStamp?
You'd do better NOT relying on collectionview sorting and NOT presenting 10,000 items to a listview.
The answer:
By default, when you’re using a CollectionView(Source) to do sorting, grouping and filtering in an itemscontrol, the sorting/grouping/filtering behavior will update when you explicitly refresh CollectionView or
when you add or remove an item to the collection.
You should therefore not sort the collection. I think your best bet is likely to instead insert at position zero of an un sorted observablecollection.
If you're inserting lots of items quickly then that's still going to burn through cycles. These are on your UI thread so you best not plan on anyone trying to interact with your UI. If these messages come in fast enough you will just have a blur anyhow.
I think you should probably instead only show the user the latest few messages in a "live" view. Don't let them sort.
If they need to see 10,000 then put that in another view that shows them a static snapshot. See what your performance is like and think about iterating when you have something concrete to play round with.