Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Sorting a String array (alphabetically)

What is the best way to go about this?
For what I'm dealing with, my array will probably only have a maximum of...lets say 50 entries.
How can I sort it aphabetically?
[192 byte] By [chupacabra] at [2007-11-11 10:06:49]
# 1 Re: Sorting a String array (alphabetically)
http://www.google.com/search?q=vb+sort+array
Phil Weber at 2007-11-11 17:23:15 >
# 2 Re: Sorting a String array (alphabetically)
yeah i've done some searching..

i have this already, which i use for integer arrays:

Sub SelectionSortNumbers(vArray As Variant)
'=============================================================
' Sorts numbers in an Integer array with a Selection Sort
'=============================================================
Dim lLoop1 As Integer
Dim lLoop2 As Integer
Dim lMin As Integer
Dim lTemp As Integer

Debug.Print "Sorting array..."

For lLoop1 = LBound(vArray) To UBound(vArray) - 1
lMin = lLoop1
For lLoop2 = lLoop1 + 1 To UBound(vArray)
If vArray(lLoop2) < vArray(lMin) Then lMin = lLoop2
Next lLoop2
lTemp = vArray(lMin)
vArray(lMin) = vArray(lLoop1)
vArray(lLoop1) = lTemp
Next lLoop1

Debug.Print "Finished sorting."

End Sub



and i found this just a moment ago:

Public Function SortArray(ByRef TheArray As Variant)
Sorted = False
Do While Not Sorted
Sorted = True
For X = 0 To UBound(TheArray) - 1
If TheArray(X) > TheArray(X + 1) Then
Temp = TheArray(X + 1)
TheArray(X + 1) = TheArray(X)
TheArray(X) = Temp
Sorted = False
End If
Next X
Loop
End Function



would i need to use Chr() or something to compare strings, or would it work as is?
chupacabra at 2007-11-11 17:24:15 >
# 3 Re: Sorting a String array (alphabetically)
I found this:
http://www.freevbcode.com/ShowCode.asp?ID=965

looks promising
chupacabra at 2007-11-11 17:25:12 >
# 4 Re: Sorting a String array (alphabetically)
it's not too match 50 .. do u want to sort them in vb ? simplist method is to put them into a autosorted listbox .
otherwise the dictionairy order is to check first character then the second and so on :
the function strcmp() will help u doing that .
Amahdy at 2007-11-11 17:26:18 >
# 5 Re: Sorting a String array (alphabetically)
WAW three posts in the time that I'm was posting mine !!
I'm was too late , anyway I found that u got it gr8 .
Amahdy at 2007-11-11 17:27:13 >
# 6 Re: Sorting a String array (alphabetically)
WAW three posts in the time that I'm was posting mine !!
I'm was too late , anyway I found that u got it gr8 .

haha, yep.

Thanks for your help! :)
chupacabra at 2007-11-11 17:28:23 >