'DeclarationPublic Property SortComparer As IComparer
'UsageDim instance As DataGridItemPropertyBase Dim value As IComparer instance.SortComparer = value value = instance.SortComparer
public IComparer SortComparer {get; set;}
'DeclarationPublic Property SortComparer As IComparer
'UsageDim instance As DataGridItemPropertyBase Dim value As IComparer instance.SortComparer = value value = instance.SortComparer
public IComparer SortComparer {get; set;}
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).
<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 ); } } } }
.NET: net5.0, net5.0-windows, net6.0, net6.0-macos, net6.0-windows, net7.0, net7.0-macos, net7.0-windows, net8.0, net8.0-browser, net8.0-macos, net8.0-windows, net9.0, net9.0-browser, net9.0-macos, net9.0-windows, net10.0, net10.0-browser, net10.0-macos, net10.0-windows.
.NET Framework: net40, net403, net45, net451, net452, net46, net461, net462, net463, net47, net471, net472, net48, net481.