ANStrSort2

Sort list of strings in ascending or descending order using pure VBA, no dependency on any outside library.
Works with list of numbers or strings or combination of both.
It uses Collection to create new list that is sorted
Pass string of items, separated by separator, along with sort order, and the separator to be used, function will return string of items listed in ascending or descending order.

This can be considered another copy of List_ItemsSort function from ANStrList functions but without using outside library

CodeFunctionName
What is this?

Public

Tested

Original Work
Function ANStrSort2(ANStrList, Optional Asc1_Desc2 = 1, Optional Sepa = "|")
Dim Coo As New Collection
Ar = Split(ANStrList, Sepa)
' Sort Asc
X1 = 0
For Each Nu In Ar
'Nu2 = Val(Nu)
Nu2 = Nu
X1 = X1 + 1
If X1 = 1 Then
Coo.Add Nu2
Else
CooX1 = 0
Add2B4 = 0
For Each Cu In Coo
CooX1 = CooX1 + 1
If Asc1_Desc2 = 1 Then
If Nu2 < Cu Then
Add2B4 = CooX1
Exit For
End If
Else
If Nu2 > Cu Then
Add2B4 = CooX1
Exit For
End If
End If
Next
If Add2B4 = 0 Then
Coo.Add Nu2
Else
Coo.Add Nu2, , Add2B4
End If
End If
Next
Rett = ""
For I = 1 To Coo.Count
If Rett > "" Then Rett = Rett & Sepa
Rett = Rett & Coo(I)
Next
ANStrSort2 = Rett
End Function

ANStrList, Optional Asc1_Desc2 = 1, Optional Sepa = "|"

? anstrsort2("31|3|f|anmar|bold|Bold codes")
3|31|Bold codes|anmar|bold|f
? anstrsort2("31|3|f|anmar|bold|Bold codes",2)
f|bold|anmar|Bold codes|31|3

Views 869

Downloads 325

CodeID
DB ID

ANmarAmdeen
609
Attachments
Revisions

v1.0

Thursday
September
24
2020