Version

Custom Sort

Please note that this control has been retired and is now obsolete to the XamDataGrid control, and as such, we recommend migrating to that control. It will not be receiving any new features, bug fixes, or support going forward. For help or questions on migrating your codebase to the XamDataGrid, please contact support.

Before you Begin

In some situations it may not be ideal to use the default sorting algorithm. With the xamGrid™ control you can easily implement custom sort logic. You can get or set a custom IComparer template object using the Column object’s SortComparer property.

This topic will show you how to create your own custom IComparer implementation and how to attach it to a column.

Assumptions:

This topic assumes that you already have a xamGrid control bound to data on your page. For more information see the Binding xamGrid to Data topic.

What You Will Accomplish

In this walkthrough you will learn how to create a custom sort by implementing the IComparer interface. This walkthrough creates a class named MyCustomSort which has been designed to sort on the last character instead of the typical first character of the string.

Follow these Steps

  1. Add a Microsoft Checkbox control to your page so that your end user can choose to use the custom sort. Set the following properties:

    • x:Name = myCheckBox

    • Content = Custom Sort

    • Checked = myCheckBox_Checked

    • Unchecked = myCheckBox_Unchecked

In XAML:

<CheckBox x:Name="myCheckBox" Content="Custom Sort"
   Checked="myCheckBox_Checked" Unchecked="myCheckBox_Unchecked">
</CheckBox>
  1. Create a class named MyCustomSort that implements the IComparer interface. This class contains code that implements the Compare method that is a member of the IComparer interface.

In Visual Basic:

Public Class MyCustomSort
    Implements IComparer(Of String)
   Public Function Compare(ByVal x As String, ByVal y As String) As Integer _
    Implements IComparer(Of String).Compare
      Dim x1, y1 As String
      ' Get the last character of the string to sort on
      x1 = x.Substring((x.Length - 1))
      y1 = y.Substring((y.Length - 1))
      Return x1.CompareTo(y1)
   End Function
End Class

In C#:

public class MyCustomSort : IComparer<string>
{
   public int Compare(string x, string y)
   {
      string x1, y1;
      //Get the last character of the string to sort on
      x1 = x.Substring(x.Length - 1);
      y1 = y.Substring(y.Length - 1);
      return x1.CompareTo(y1);
   }
}
  1. Within the myCheckBox_Checked event, set the SortComparer property of the ProductName column to the MyCustomSort object.

In Visual Basic:

Dim customSortColumn As Column = Me.dataGrid.Columns.DataColumns("ProductName")
customSortColumn.SortComparer = New MyCustomSort()

In C#:

private void myCheckBox_Checked(object sender, RoutedEventArgs e)
{
   Column customSortColumn = this.MyGrid.Columns.DataColumns["ProductName"];
   customSortColumn.SortComparer = new MyCustomSort();
}
  1. Within the myCheckBox_Unchecked event, clear the SortComparer property.

In Visual Basic:

Dim clearCustomSortColumn As Column = Me.MyGrid.Columns.DataColumns("ProductName")
clearCustomSortColumn.SortComparer = Nothing

In C#:

private void myCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
   Column clearCustomSortColumn = this.MyGrid.Columns.DataColumns["ProductName"];
   clearCustomSortColumn.SortComparer = null;
}
  1. Save and run your project.

Custom Sort