Any good suggestion how to sort a tree list with dot-notation numbers?
For examples (sorted):
1
1.1
1.1.1
1.1.2
1.2
2
2.1
2.1.1
2.1.2.1
2.1.2.2
My ideas:
-Make an 2d-array with splited numbers and loop trough
-Remove dots, search for biggest number and add zeros to the rest (1000,1100,1110,1200,2000,2100,2110,…) and sort it
dim arr() as string = array("2.1.15.1", "1.1", "1.1.1", "1.2", "2", "2.1", "2.1.1", "2.1.9.2", "2.1.2.1", "2.1.2.2", "1", "1.1.2")
dim TmpArr() as String
dim tmpStr as string
dim i as integer
dim TmpSplit(-1) as String
for each tmpstr in arr
TmpSplit = Split(tmpStr, ".")
for i = 0 to UBound(TmpSplit)
TmpSplit(i) = Format(val(TmpSplit(i)), "00000000000")
next
TmpArr.Append join(TmpSplit, ".")
next
TmpArr.SortWith(Arr)
Here is Alain’s algorithm wrapped into a reusable method…
Sub SortNumbers(ByRef numberArr() As String)
dim TmpArr() as String
dim tmpStr as string
dim i as integer
dim TmpSplit(-1) as String
for each tmpstr in numberArr
TmpSplit = Split(tmpStr, ".")
for i = 0 to UBound(TmpSplit)
TmpSplit(i) = Format(val(TmpSplit(i)), "00000000000")
next
TmpArr.Append join(TmpSplit, ".")
next
numberArr.SortWith(TmpArr)
End Sub
My bad… thanks for pointing out the bug… here is the fixed method…
Sub SortNumbers(ByRef numberArr() As String)
dim TmpArr() as String
dim tmpStr as string
dim i as integer
dim TmpSplit(-1) as String
for each tmpstr in numberArr
TmpSplit = Split(tmpStr, ".")
for i = 0 to UBound(TmpSplit)
TmpSplit(i) = Format(val(TmpSplit(i)), "00000000000")
next
TmpArr.Append join(TmpSplit, ".")
next
TmpArr.SortWith(numberArr)
End Sub