Here's a comparer I wrote awhile back to implement natural sorting like Windows uses.
If you'd rather use the shell api to do it you can implement it like this:
vb.net Code:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim itemList As New List(Of String) From {"item_1", "item_2", "item_4", "item_10", "item_8", "item_11"} itemList.Sort(New NaturalComparerUsingAPI(False)) End Sub End Class Public Class NaturalComparerUsingAPI Implements IComparer(Of String) Private Declare Unicode Function StrCmpLogicalW Lib "shlwapi.dll" (s1 As String, s2 As String) As Integer Private ReadOnly _order As Integer Public Sub New(Optional Ascending As Boolean = True) _order = If(Ascending, 1, -1) End Sub Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare Return StrCmpLogicalW(x, y) * _order End Function End Class




Reply With Quote