Improving Microsoft DataGrid CTP sorting performance – Part 2


In this blog I wrote how you can improve Microsoft DataGrid CTP by providing your own custom sort.  
As one of the comments suggested you can get even better performance by using delegates.
I have changed MySort code below and I now use delegates to avoid the switch comparison that was called on every compare during the QuickSort . I can now see additional ~50% speed gain during sort.


In my 300,000 rows DataGrid, before I saw sort speed decreased from almost 5 minutes to 2.4 seconds and now the String type columns (e.g. Name, Position) are sorted in 1.4 sec while the Integer type columns (e.g. Id) are now sorted in 400 ms!


   1:  public class MySort : IComparer
   2:  {
   3:        public delegate int TwoArgDelegate(Employee arg1, Employee arg2);
   4:        TwoArgDelegate myCompare;
   5:        public MySort(ListSortDirection direction, DataGridColumn column)
   6:        {
   7:                  int dir = (direction == ListSortDirection.Ascending) ? 1 : -1;
   8:                  //set a delegate to be called by IComparer.Compare
   9:                  switch ((string)column.Header)
  10:                  {
  11:                      case “Id”:
  12:                          myCompare = (a, b) => { return a.Id.CompareTo(b.Id) * dir; };
  13:                          break;
  14:                      case “Name”:
  15:                          myCompare = (a, b) => { return a.Name.CompareTo(b.Name) * dir; };
  16:                          break;
  17:                      case “Position”:
  18:                          myCompare = (a, b) => { return a.Position.CompareTo(b.Position) * dir; };
  19:                          break;
  20:                      case “Tel”:
  21:                          myCompare = (a, b) => { return a.Telephone.CompareTo(b.Telephone) * dir; };
  22:                          break;
  23:                      case “Email”:
  24:                          myCompare = (a, b) => { return a.Email.CompareTo(b.Email) * dir; };
  25:                          break;
  26:                      case “Enabled”:
  27:                          myCompare = (a, b) => { return a.Enabled.CompareTo(b.Enabled) * dir; };
  28:                          break;
  29:                      case “City”:
  30:                          myCompare = (a, b) => { return a.City.CompareTo(b.City) * dir; };
  31:                          break;
  32:                      case “Country”:
  33:                          myCompare = (a, b) => { return a.Country.CompareTo(b.Country) * dir; };
  34:                          break;
  35:                      default:
  36:                          myCompare = (a, b) => { return 0; };
  37:                          break;
  38:                  }
  39:              }
  40:              int IComparer.Compare(object X, object Y)
  41:              {
  42:                  return myCompare((Employee)X, (Employee)Y);
  43:              }
  44:      }
  45:  }

DataGridSort2.zip

Comments (5)

  1. As it was announced at the PDC 2008 keynote, the Microsoft WPF DataGrid V1 is now released and available

  2. blindmeis says:

    Hi,

    how can i use custom sorting when i can just cast to an BindingListCollectionView? I bind the grid to a DataTable and cant cast to ListCollectionView.

    any ideas?

  3. bear831204 says:

    Hi,

    very good sample!I have a question, your sample only support a single column sorting,

    if I pressing the "shift" key and sorting by

    multiple columns, then you know the previous

    sorting logic is overriden by the new one,

    how to solve this? Any idea?

  4. Nice, thanks!

    Could you please explain the time measuring trick with dispatching a method call? Why Normal priority? How does it work?

    Thanks!