Home > Net >  Sorting comma-delimited string alphabetically in a smart way
Sorting comma-delimited string alphabetically in a smart way

Time:09-07

I have a string that looks like

"Carol, Ernie, Alice, Bob, Dave"

I want to pass it to a VBA function that sorts it so it becomes

"Alice, Bob, Carol, Dave, Ernie"

Is there a smart way to do this with built in functions that doesn't require a lot of custom code or am I out of luck?

CodePudding user response:

Using the idea at https://stackoverflow.com/a/45379461/2193968

This should work:

Function SortCSV(Value As String)
    Set Arr = CreateObject("System.Collections.ArrayList")
    For Each Item In Split(Value, ",")
        Arr.Add Trim(Item)
    Next
    Arr.Sort
    SortCSV = Join(Arr.ToArray, ",")
    Set Arr = Nothing
End Function
  • Related