'// Argument
' aryIO
'// Function
Dim PartCompare As Func(Of String, String, Integer) =
Function(left As String, right As String) As Integer
Dim x, y As Integer
If Not(Integer.TryParse(left, x)) Then
Return left.CompareTo(right)
ElseIf Not(Integer.TryParse(right, y)) Then
Return left.CompareTo(right)
End If
Return x.CompareTo(y)
End Function
Dim NaturalCompare As Func(Of String, String, Integer) =
Function(x As String, y As String) As Integer
Dim table As New System.Collections.Generic.Dictionary(Of String, String())
If x = y Then
Return 0
End If
Dim x1, y1 As String()
If Not(table.TryGetValue(x, x1)) Then
x1 = System.Text.RegularExpressions.Regex.Split(x.Replace(" ", ""), "([0-9]+)")
table.Add(x, x1)
End If
If Not(table.TryGetValue(y, y1)) Then
y1 = System.Text.RegularExpressions.Regex.Split(y.Replace(" ", ""), "([0-9]+)")
table.Add(y, y1)
End If
Dim i As Integer = 0
While i < x1.Length AndAlso i < y1.Length
If x1(i) <> y1(i) Then
Return PartCompare(x1(i), y1(i))
End If
i += 1
End While
If y1.Length > x1.Length Then
Return 1
Else If x1.Length > y1.Length Then
Return -1
Else
Return 0
End If
Return 0
End Function
'// Main
For idxA As Integer = 0 To UBound(aryIO) -1
For idxB As Integer = idxA + 1 To UBound(aryIO)
If NaturalCompare(aryIO(idxA), aryIO(idxB)) > 0 Then
Dim wrk As String = aryIO(idxB)
aryIO(idxB) = aryIO(idxA)
aryIO(idxA) = wrk
End If
Next
Next