Xceed Toolkit Plus for WPF v5.0 Documentation
In This Topic
    Providing a custom sort comparer
    In This Topic

    The following example demonstrates how to provide a custom sort comparer that sorts addresses. The AddressComparer class (provided below) will first sort addresses which begin with numeric values by street name and then civic number. Address that do not have a civic number will be sorted alphabetically.

    XAML
    Copy Code
    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
          xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
       <Grid.Resources>
          <local:AddressComparer x:Key="addressComparer"/>
          <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                             Source="{Binding Source={x:Static Application.Current},
                                                              Path=Orders}"
                                             AutoCreateItemProperties="False">
             <xcdg:DataGridCollectionViewSource.ItemProperties>
                <xcdg:DataGridItemProperty Name="ShipCountry" />
                <xcdg:DataGridItemProperty Name="ShipCity" />
                <xcdg:DataGridItemProperty Name="ShipAddress"
                                           SortComparer="{StaticResource addressComparer}"/>
                <xcdg:DataGridItemProperty Name="ShipVia" />
             </xcdg:DataGridCollectionViewSource.ItemProperties>
          </xcdg:DataGridCollectionViewSource>
       </Grid.Resources>
       <xcdg:DataGridControl x:Name="OrdersGrid"
                             ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>        
    </Grid>
    VB.NET
    Copy Code
    Dim collectionView As New DataGridCollectionView( Orders, GetType( System.Data.DataRow ), False, False )
    collectionView.ItemProperties.Add( New DataGridItemProperty( "ShipCountry", GetType( String ) ) )
    collectionView.ItemProperties.Add( New DataGridItemProperty( "ShipCity", GetType( String ) ) )
    collectionView.ItemProperties.Add( New DataGridItemProperty( "ShipVia", GetType( Integer ) ) )
    Dim shipAddress As New DataGridItemProperty( "ShipAddress", GetType( String ) )
    shipAddress.SortComparer = New AddressComparer()
    collectionView.ItemProperties.Add( shipAddress )
    dataGridControl.ItemsSource = collectionView
    C#
    Copy Code
    DataGridCollectionView collectionView = new DataGridCollectionView( Orders, typeof( System.Data.DataRow ), false, false );
    collectionView.ItemProperties.Add( new DataGridItemProperty( "ShipCountry", typeof( string ) ) );
    collectionView.ItemProperties.Add( new DataGridItemProperty( "ShipCity", typeof( string ) ) );
    collectionView.ItemProperties.Add( new DataGridItemProperty( "ShipVia", typeof( int ) ) );
    DataGridItemProperty shipAddress = new DataGridItemProperty( "ShipAddress", typeof( string ) );
    shipAddress.SortComparer = new AddressComparer();
    collectionView.ItemProperties.Add( shipAddress );
    dataGridControl.ItemsSource = collectionView;

    The following code provides the implementation of the AddressComparer class.

    VB.NET
    Copy Code
    Imports System
    Imports System.Collections
    Imports System.Data
    Namespace Xceed.Wpf.Documentation
      Public Class AddressComparer
                   Implements IComparer
        Public Sub New()
        End Sub
        Public Function Compare( x As Object, y As Object ) As Integer Implements IComparer.Compare
          Dim stringX As String = CType( x, String )
          Dim stringY As String = Ctyle( y, String )
          Const digits As String = "0123456789"
          If( ( digits.IndexOf( stringX( 0 ) ) >= 0 ) And ( digits.IndexOf( stringY( 0 ) ) >= 0 ) ) Then
            Dim index As Integer = 0
            Dim xNumber As System.Text.StringBuilder = New System.Text.StringBuilder()
            While( ( index < stringX.Length ) And ( digits.IndexOf( stringX( index ) ) >= 0 ) )
              xNumber.Append( stringX( index ) )
              index++
            End While
            index = 0
            Dim yNumber As System.Text.StringBuilder = New System.Text.StringBuilder()
            While( ( index < stringY.Length ) And ( digits.IndexOf( stringY( index ) ) >= 0 ) )
              yNumber.Append( stringY( index ) )
              index++
            End While
            Dim xValue = Long.Parse( xNumber.ToString() )
            Dim yValue As Long = Long.Parse( yNumber.ToString() )
            If( xValue > yValue ) Then
              Return 1
            End If
            If( xValue < yValue ) Then
              Return -1
            End If
            Return stringX.CompareTo( stringY )
          Else
            Return stringX.CompareTo( stringY )
          End If
        End Function
      End Class
    End Namespace
    C#
    Copy Code
    using System;
    using System.Collections;
    using System.Data;
    namespace Xceed.Wpf.Documentation
    {
     public class AddressComparer: IComparer
     {
       public AddressComparer()
       {
       }
       int IComparer.Compare( object x, object y )
       {
         string stringX = ( string )x;
         string stringY = ( string )y;
         const string digits = "0123456789";
         if( ( digits.IndexOf( stringX[ 0 ] ) >= 0 ) && ( digits.IndexOf( stringY[ 0 ] ) >= 0 ) )
         {
           int index = 0;
           System.Text.StringBuilder xNumber = new System.Text.StringBuilder();
           while( ( index < stringX.Length ) && ( digits.IndexOf( stringX[ index ] ) >= 0 ) )
           {
             xNumber.Append( stringX[ index ] );
             index++;
           }
           index = 0;
           System.Text.StringBuilder yNumber = new System.Text.StringBuilder();
           while( ( index < stringY.Length ) && ( digits.IndexOf( stringY[ index ] ) >= 0 ) )
           {
             yNumber.Append( stringY[ index ] );
             index++;
           }
           long xValue = long.Parse( xNumber.ToString() );
           long yValue = long.Parse( yNumber.ToString() );
           if( xValue > yValue )
             return 1;
           if( xValue < yValue )
             return -1;
           return stringX.CompareTo( stringY );
         }
         else
         {
           return stringX.CompareTo( stringY );
         }
       }
     }
    }