Xceed DataGrid for WPF v7.2 Documentation
SortComparer Property (DataGridItemPropertyBase)
Example 


Xceed.Wpf.DataGrid Assembly > Xceed.Wpf.DataGrid Namespace > DataGridItemPropertyBase Class : SortComparer Property
Gets or sets the custom data comparer that will be used to sort the values of the data-grid item property's associated column.
Syntax
'Declaration
 
Public Property SortComparer As IComparer
'Usage
 
Dim instance As DataGridItemPropertyBase

Dim value As IComparer

 

instance.SortComparer = value

 

value = instance.SortComparer
public IComparer SortComparer {get; set;}

Property Value

The IComparer that will be used to sort the values of the DataGridItemProperty's associated column. Can be a null reference (Nothing in Visual Basic).
Remarks

If a null reference (Nothing in Visual Basic), the default comparer will be used.

Custom sort comparers are not supported by the virtualizing collection views (e.g., DataGridVirtualizingCollectionViewSource).

Example
All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.
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.The following code provides the implementation of the AddressComparer class.The following code provides the implementation of the AddressComparer class.
<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}}">          

      <xcdg:DataGridControl.View> 

         <xcdg:TableView ColumnStretchMode="StretchAll"/> 

      </xcdg:DataGridControl.View> 

   </xcdg:DataGridControl> 

</Grid>
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
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 );

    }

  }

}

}
Requirements

Target Platforms: Windows 11, Windows, 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

DataGridItemPropertyBase Class
DataGridItemPropertyBase Members