Sorting Array Entries

RhinoNeil

Board Regular
Joined
Dec 16, 2010
Messages
54
I have an array which contains simple strings. E.g.

dim a() as string
a(0) = "B"
a(1) = "G"
a(2) = "F"
a(3) = "A"
a(4) = "C"

Is there an easy (and simple) way to sort the array entries into alphabetical order (either as they are entered into the array or after it is populated)?

Thanks
 
Last edited by a moderator:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Re: Srting Array Entries

You can either dump the array onto a sheet and sort it there, or use a sorting algorithm. Here's a simple Bubble sort function:
Code:
Public Function BubbleSort(Strings) As Variant

    Dim a As Long
    Dim E As Long, f As Long, g As Long

    Dim i, j
    Dim m As Variant, n As Variant
    
    E = 1
    n = Strings
    Do While E <> -1
    
        For a = 0 To UBound(Strings) - 1
            i = n(a)
            j = n(a + 1)
            f = StrComp(i, j)
            If f <= 0 Then
                n(a) = i
                n(a + 1) = j
            Else
                n(a) = j
                n(a + 1) = i
                g = 1
            End If
        Next a
        If g = 1 Then
            E = 1
        Else
            E = -1
        End If
        
        g = 0
    Loop
    BubbleSort = n
End Function
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top