I am looking to do the following, but am unsure how...
* Sort an array of strings
* Delete duplicate items from that sorted array
Anybody have any ideas?
Thanks!!
Printable View
I am looking to do the following, but am unsure how...
* Sort an array of strings
* Delete duplicate items from that sorted array
Anybody have any ideas?
Thanks!!
For your first question. Do a bubble sort.
Code:Dim List(1 To 5) As String 'the array
Dim Swap As String 'temporary
Dim I As Integer 'for loops
Dim J As Integer 'for loops
List(1) = "Hello"
List(2) = "Bye"
List(3) = "Lala"
List(4) = "Blabla"
List(5) = "Kaboom"
'sort them
For I = 1 To UBound(List)
'the bubble sort moves large values to the top
For J = 1 To UBound(List) - 1
'compare them
If List(J) > List(J + 1) Then
'list(x)>list(x+1)
Swap = List(J + 1)
List(J + 1) = List(J)
List(J) = Swap
End If
Next
Next
What sorting method do you want to use?
The bubble sort is a simple (and slow) method of sorting. If you need speed, then there are more complex (and faster) methods of sorting.
Bubble sort will work. I changed the code to remove duplicates
[code]
Dim Count As Integer
For I = 1 To UBound(List)
'the bubble sort moves large values to the top
For J = 1 To UBound(List) - 1
'compare them
If List(J) > List(J + 1) Then
'list(x)>list(x+1)
Swap = List(J + 1)
List(J + 1) = List(J)
List(J) = Swap
Else
If List(J) = List(J+1) Then
For Count = (J + 1) To 4
List(Count) = List(Count + 1)
Next
End If
End If
Next
Next
Above, you'll have to make sure I doesn't compare number 5 now because then it will move spaces to the front!
Sorry, only saw this now!
Marnitzg, while you posted that, I wrote some code for removing duplicates from an array. (it's long)
Code:Dim List() As String
ReDim List(1 To 6) As String 'the array
Dim Doubles As Long 'the number of duplicate items
Dim Swap As String 'temporary
Dim I As Integer 'for loops
Dim J As Integer 'for loops
Dim Counter As Integer
List(1) = "Hello"
List(2) = "Bye"
List(3) = "Hello"
List(4) = "Blabla"
List(5) = "Hello"
List(6) = "Bye"
'count the duplicates
For I = 1 To UBound(List)
'are there more then this one?
For J = (I + 1) To UBound(List)
'search for another one
If List(J) = List(I) Then
'it will also found duplicate ""'s
'don't count them
If List(J) = "" Then
Else
'duplicate item found
'set the list(j) to ""
List(J) = ""
Doubles = Doubles + 1
End If
End If
Next
Next
'delete the duplicates
ReDim List2(1 To UBound(List) - Doubles) As String
Counter = 1
For I = 1 To UBound(List)
'if it isn't a duplicate, copy it
If List(I) <> "" Then
List2(Counter) = List(I)
'add one to the index counter
Counter = Counter + 1
End If
Next
ReDim List(1 To UBound(List2)) As String
'copy list2 to list and kill list2
For I = 1 To UBound(List)
List(I) = List2(I)
Next
'kill list2
ReDim List2(1 To 2) As String
Bubble sort is one big slow monster when it comes to larger arrays, here's shell sort that should sort a lot faster:
Code:Sub Sort_shell(a() As String)
Dim n&, i&, j&, k&, h
n = UBound(a)
k = n \ 2
While k > 0
For i = 0 To n - k
j = i
While (j >= 0) And (a(j) > a(j + k))
h = a(j)
a(j) = a(j + k)
a(j + k) = h
If j > k Then
j = j - k
Else
j = 0
End If
Wend
Next i
k = k \ 2
Wend
End Sub
Thanks, everyone!!
Looks like this will work...
I'll take a look at the 2 pieces of code that were posted near the end, too...
Thanks!!