Hi @StewartBW ,
You can use a lambda expression or a custom sorting logic inline.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pathname As String = "testfolders"
Dim directories As String() = Directory.GetDirectories(pathname)
Array.Sort(directories, AddressOf CompareNaturally)
For Each dir As String In directories
Debug.WriteLine(Path.GetFileName(dir))
Next
End Sub
Private Function CompareNaturally(x As String, y As String) As Integer
Dim xName As String = Path.GetFileName(x)
Dim yName As String = Path.GetFileName(y)
Dim xParts As String() = Regex.Split(xName, "(\d+)")
Dim yParts As String() = Regex.Split(yName, "(\d+)")
For i As Integer = 0 To Math.Min(xParts.Length, yParts.Length) - 1
If xParts(i) <> yParts(i) Then
Dim xPart As Integer
Dim yPart As Integer
If Integer.TryParse(xParts(i), xPart) AndAlso Integer.TryParse(yParts(i), yPart) Then
Return xPart.CompareTo(yPart)
Else
Return xParts(i).CompareTo(yParts(i))
End If
End If
Next
Return xParts.Length.CompareTo(yParts.Length)
End Function
Best Regards.
Jiachen Li
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.