A much faster sorting for ListView in WPF

July 31, 2007

In my first part of performance tuning techniques, I’ve reviewed some general approaches. Now I want to show you how to fine tune your ListView for sorting and displaying HUGE amount of rows. In this demo, I will show you that by using a custom sorter and virtualizingstackpanel, your sorting speed is about 100 times faster!


First you need to specify the above property to true in your ListView, (this is the default value for ListView in WPF).

Then next, you need to use custom sorter, instead of SortDescriptions as described in my earlier blog. The key is using the CustomSort property of ListCollectionView:
ListCollectionView view = (ListCollectionView)CollectionViewSource.GetDefaultView(myListView.ItemsSource);

Then in your ColumnHeader click event handler, you add something like the following:

view.CustomSort = sorter;

Where sorter is a custom class you implement the IComparer interface.

Here is the demo of this custom (much faster!) sort program to sort 200,000 rows in a virtualized ListView in WPF:

In the demo, you see the custom sort takes about only 1.6 – 2 seconds to sort. I tried this 200,000 rows with SortDescriptions, and it takes about 3 minutes (~ 167 – 180 seconds) to sort this many rows. This is tested on both the .Net Framework 3.0 and the just released .Net Framework 3.5 Beta 2. Now you can enjoy a much more scalable ListView in WPF. 🙂

[Update 8/1/2007]

I’ve uploaded the project source code to demonstrate this sorting technique. You can get it here. You’ll need Visual Studio 2008 Beta 2 to open the solution file (or project file).

[Update 8/3/2007]

A modified version of the sample to support sorting multiple columns is posted here.  You’ll need Visual Studio 2008 Beta 2 to open the solution file (or project file).

[Updated: 1/16/2009]

The new source code link is updated at:

new source code link

Performance tuning of WPF Applications (Part I)

July 24, 2007

These days as I am digging more into WPF and XAML into more complex applications, I’ve got hit more and more by performance and scalability issues. So I did some research over the past few days and came up with the following list of links that I hope you can find them helpful in your own XAML/WPF applications.

  • Optimizing Performance w/ StreamGeometry. If you are doing a lot of creating/destroying geometric shapes, such as 2D charts, data clouds in your application, then this article may worth a reading.
  • Here is a more complete overview and a set of guidelines for optimizing your WPF application. Of this article, I find the following sections particular useful in enterprise-oriented WPF applications: software rendering pipeline considerations, freezable elements, different layout panel container considerations when building very complex UI.
  • Consider XamlReader and XamlWriter for faster duplication of many identical UI across different domains or windows.

I will write more about my thoughts on performance tuning in later parts of this series.

In the meantime, I highly recommend you try the Performance Profiler for WPF to profile your complex WPF application and see where your performance bottleneck is.