|
-
Oct 29th, 2000, 12:28 PM
#1
Thread Starter
Hyperactive Member
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!!
-
Oct 29th, 2000, 01:00 PM
#2
Fanatic Member
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
-
Oct 29th, 2000, 01:01 PM
#3
Hyperactive Member
What sorting method do you want to use?
-
Oct 29th, 2000, 01:03 PM
#4
Fanatic Member
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.
-
Oct 29th, 2000, 01:08 PM
#5
Hyperactive Member
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
-
Oct 29th, 2000, 01:10 PM
#6
Hyperactive Member
Ooops
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!
-
Oct 29th, 2000, 01:32 PM
#7
Fanatic Member
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
-
Oct 29th, 2000, 10:05 PM
#8
transcendental analytic
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 29th, 2000, 10:35 PM
#9
Thread Starter
Hyperactive Member
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!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|