SBendBuckeye,
Now this sorts up to 5 similiar letters, within 2 words.
Once you see the structure, you can go as high as you like.
Problem though, run-time error occurs if word length is not long enough for Mid() function. Example; Niger & Nigeria, Niger should come first, but error occurs on ...
Asc(Mid(sArray(z), 6, 1))
Tried Nz(), but to no avail.
I know I can use If Len(sArray(x)) = 5 ....
But, no patience this evening.
Sub SortArray()
Dim x, z, y As Integer, sArray, sNewArray() As Variant, bLess As Boolean
sArray = Array("Barbuda", "Kenya", "Denmark", "Western Sahara", "Guatemala", "Japan", "Oceania", "Zimbabwe", _
"Uraguay", "Italy", "Malaysia", "Syria", "Nigeria", "Barbados", "Qatar", "Holland", "Bulgaria", _
"Canada", "Uganda", "Lebanon", "Rwanda", "Nepal", "Ireland", "England", "Iceland", "France", _
"Yemin", "Peru", "Guadeloupe", "Taiwan", "Germany", "Taipan", "Niger", "Cambodia", "Vietnam", "Algeria")
'...then populate a new array.
ReDim sNewArray(0 To 1)
If Asc(Left(sArray(0), 1)) > Asc(Left(sArray(1), 1)) Then
sNewArray(0) = sArray(1): sNewArray(1) = sArray(0)
Else
sNewArray(0) = sArray(0): sNewArray(1) = sArray(1)
End If
'Loop thru new array. Since it will loop lowest to highest,
'The minute sArray(2), is lower then sNewArray(x), you will know exactly where to place it.
For z = 2 To UBound(sArray)
For x = 0 To UBound(sNewArray)
If Asc(Left(sArray(z), 1)) = Asc(Left(sNewArray(x), 1)) Then
If Asc(Mid(sArray(z), 2, 1)) < Asc(Mid(sNewArray(x), 2, 1)) Then
GoTo Less
ElseIf Asc(Mid(sArray(z), 2, 1)) = Asc(Mid(sNewArray(x), 2, 1)) Then
If Asc(Mid(sArray(z), 3, 1)) < Asc(Mid(sNewArray(x), 3, 1)) Then
GoTo Less
ElseIf Asc(Mid(sArray(z), 3, 1)) = Asc(Mid(sNewArray(x), 3, 1)) Then
If Asc(Mid(sArray(z), 4, 1)) < Asc(Mid(sNewArray(x), 4, 1)) Then
GoTo Less
ElseIf Asc(Mid(sArray(z), 4, 1)) = Asc(Mid(sNewArray(x), 4, 1)) Then
If Asc(Mid(sArray(z), 5, 1)) < Asc(Mid(sNewArray(x), 5, 1)) Then GoTo Less Else GoTo Continue
End If
End If
End If
End If
If Asc(Left(sArray(z), 1)) < Asc(Left(sNewArray(x), 1)) Then
Less:
bLess = True
ReDim Preserve sNewArray(0 To UBound(sNewArray) + 1)
For y = UBound(sNewArray) - 1 To x Step -1
sNewArray(y + 1) = sNewArray

'reAdjust all "higher than x" elements in array, make room for new element
Next y
sNewArray(x) = sArray(z) 'Assign new element, to current position of new Array
Exit For 'no need to continue looping
End If
Continue:
Next x
If bLess = False Then
ReDim Preserve sNewArray(0 To UBound(sNewArray) + 1)
sNewArray(UBound(sNewArray)) = sArray(z) 'append to Ubound of Array
End If
x = 0: bLess = False
Next z
x = 0
For x = 0 To UBound(sNewArray)
Debug.Print x & ": " & sNewArray(x)
Next x
End Sub