-3

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?

skm
  • 5,015
  • 8
  • 43
  • 104
  • 1
    Did you not try out how it behaves? What is your actual problem? – Clemens Jan 12 '23 at 15:06
  • @Clemens: My actual problem is that I don't know how is it implemented. For limited number of items (tried with 5-10), it works fine for me. But I don't know how would it react if I have thousands/millions of items. Even in my case, where it can maintain a sorted list, will it need do the complete sorting everytime a new item is added? – skm Jan 12 '23 at 15:12
  • "'*I don't know how would it react if I have thousand items*" - then write a test program that creates a few thousand items. Where exactly is the problem? We can not tell if a certain solution works for you or not. You have to judge yourself. – Clemens Jan 12 '23 at 15:14
  • @Clemens: Problem is I want to know how is it implemented/works and I am not able to find a answer. Is it bad to to try to learn about it? – skm Jan 12 '23 at 15:17
  • 1
    If you want to know how it is implement, you may take a look at the [.NET Reference Source](https://referencesource.microsoft.com/). And of course you will find out how it works when you write a test program. – Clemens Jan 12 '23 at 15:20
  • 1
    It's slower than linq would be. But you should never have thousands of items you present to the UI. Always filter. Think in terms of a maximum 200 or so. Then it doesn't matter if your collectionview sorting is slow and the user won't have to slowly slowly scroll through huge masses of data. – Andy Jan 12 '23 at 15:27

1 Answers1

0

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.

Andy
  • 11,864
  • 2
  • 17
  • 20