How to sort an alphanumeric column in DataGrid
<p>I have a DataGrid with alphanumeric values, e.g.:</p>

<ul>
<li>1 second</li>
<li>100 mile</li>
<li>1499</li>
<li>28th street</li>
<li>50 apples</li>
<li>701: a prime number</li>
<li>9</li>
<li>A banana</li>
</ul>

<p>This is how it autosorts, since all values are strings. How can I make it sort as an integer, like so:</p>

<ul>
<li>1 second</li>
<li>9</li>
<li>28th street</li>
<li>50 apples</li>
<li>100 mile</li>
<li>701: a prime number</li>
<li>1499</li>
<li>A banana</li>
</ul>

<p>I've tried with <code>SortMemberPath</code> but I'm not sure how to implement this in my codebehind.</p>

<pre class="lang-xml prettyprint-override"><code>&lt;DataGrid.Columns&gt;
    &lt;DataGridTextColumn x:Name="IdColumn" Binding="{Binding Id}" Header="Id"/&gt;
    &lt;DataGridTextColumn x:Name="NameColumn" Binding="{Binding Name}" Header="Name" SortMemberPath="NumSort"/&gt;
&lt;/DataGrid.Columns&gt;
</code></pre>

<p>I even tried implementing something like <a href="https://stackoverflow.com/a/22661243/2212396">this</a>, however property I'm trying to sort is in auto-generated code (ItemsDataSet.Designer.vb) with over 10000 lines. Not sure if it's smart to place anything there, but nonetheless I've tried, like so:</p>

<pre class="lang-vbs prettyprint-override"><code>&lt;Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")&gt;  _
Public Property Name() As String
    Get
        Return CType(Me(Me.tablet_Items.NameColumn),String)
    End Get
    Set
        Me(Me.tablet_Items.NameColumn) = Value
        If Value IsNot Nothing Then NumSort = Integer.Parse(Value.ToString())
    End Set
End Property
</code></pre>

<p>However, VS complains about <code>NumSort</code> not being declared. I'm not sure where to declare it in the code, I've tried in several places, but with no luck.</p>

<hr>

<h2>EDIT:</h2>

<p>I've made a tiny step forward. I've implemented this code that allows me to sort on two columns, but I'm still not sure how to access each record in the grid to do some comparison.</p>

<pre class="lang-vbs prettyprint-override"><code>Private Sub dataGridName_Sorting(sender As Object, e As DataGridSortingEventArgs) Handles T_MoviesDataGrid.Sorting
    e.Handled = True
    Dim cView = CollectionViewSource.GetDefaultView(sender.ItemsSource)
    Dim direction As ListSortDirection = ListSortDirection.Ascending
    If cView.SortDescriptions.FirstOrDefault().PropertyName = e.Column.SortMemberPath Then
        direction = If(cView.SortDescriptions.FirstOrDefault().Direction = ListSortDirection.Descending, ListSortDirection.Ascending, ListSortDirection.Descending)
    End If

    cView.SortDescriptions.Clear()
    If e.Column.SortMemberPath = "NameSort" Then
        AddSortColumn(DirectCast(sender, DataGrid), "Name", direction)
        AddSortColumn(DirectCast(sender, DataGrid), "Id", direction)
    End If
End Sub

Private Sub AddSortColumn(sender As DataGrid, sortColumn As String, direction As ListSortDirection)
    Dim cView = CollectionViewSource.GetDefaultView(sender.ItemsSource)
    cView.SortDescriptions.Add(New SortDescription(sortColumn, direction))
    For Each col In sender.Columns.Where(Function(x) x.SortMemberPath = sortColumn)
        col.SortDirection = direction
    Next
End Sub
</code></pre>

<p>In several of similar topics people often suggest to use IComparer. I've tried to implement on but I'm not sure how to accomplish that. This is where I'm stuck:</p>

<pre class="lang-vbs prettyprint-override"><code>Public Class MyComparing
    Inherits MyDataSet
    Implements IComparable
    Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
        Return '???
    End Function
End Class
</code></pre>

<p>Instead of inheriting from MyDataSet, I've tried to go with <code>partial</code>, but still stuck.</p>