Request code help, sorting array in memory
Works perfectly- Thanks Tom!!
:-)
Keith
"Tom Ogilvy" wrote in message
...
If you won't have any blanks in the lower indexed end of the array, and
you
want your spaces at the upper indexed area of the array, you can do:
Sub SortArray()
Dim FoundNames(1 To 100) As String
For i = 1 To 40
For j = 1 To Int(Rnd() * 10 + 1)
FoundNames(i) = FoundNames(i) & Chr(Int(Rnd * 26 + 65))
Next
Next
For i = 1 To 100
Cells(i, 2).Value = FoundNames(i)
Next
For i = 1 To 99
For j = i + 1 To 100
If FoundNames(i) FoundNames(j) And _
FoundNames(j) < "" Then
temp = FoundNames(j)
FoundNames(j) = FoundNames(i)
FoundNames(i) = temp
Debug.Print i, j, FoundNames(i), FoundNames(j)
End If
Next
Next
For i = 1 To 100
Cells(i, 1).Value = FoundNames(i)
Next
End Sub
If you want it sorted without regards to blanks:
Sub SortArray()
Dim FoundNames(1 To 100) As String
For i = 1 To 40
For j = 1 To Int(Rnd() * 10 + 1)
FoundNames(i) = FoundNames(i) & Chr(Int(Rnd * 26 + 65))
Next
Next
For i = 1 To 100
Cells(i, 2).Value = FoundNames(i)
Next
For i = 1 To 99
For j = i + 1 To 100
If FoundNames(i) FoundNames(j) Then
temp = FoundNames(j)
FoundNames(j) = FoundNames(i)
FoundNames(i) = temp
Debug.Print i, j, FoundNames(i), FoundNames(j)
End If
Next
Next
For i = 1 To 100
Cells(i, 1).Value = FoundNames(i)
Next
End Sub
but all the blanks will be at the bottom.
--
Regards,
Tom Ogilvy
"KR" wrote in message
...
I have an array (1 to 100) in memory (I'll never use more than 80-90,
but
wanted some extra room just in case). I'm trying to use the following
code
to sort the array into alphabetical order before adding the array to a
combobox, but it isn't working. I eliminated all blanks from my array,
so
these are just random names in the array (my test set has about 40
names).
Of most importance, I want to sort these names in complete alpha order,
even
though the strings are of different length (I'm not sure if that
matters,
or
how to accomodate if it does, but so far the names don't appear to be
sorting at all)
no matter how I look at it, I just can't figure out what I'm doing
wrong.
For i = 99 To 1 Step -1
For j = (i + 1) To 1 Step -1
If FoundNames(i) FoundNames(j) Then
temp = FoundNames(j)
FoundNames(j) = FoundNames(i)
FoundNames(i) = temp
End If
Next
Next
I appreciate your help,
Keith
--
The enclosed questions or comments are entirely mine and don't represent
the
thoughts, views, or policy of my employer. Any errors or omissions are
my
own.
|