Results 1 to 9 of 9

Thread: Array Questions

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Question

    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!!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    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
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  3. #3
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    What sorting method do you want to use?

  4. #4
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    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.
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  5. #5
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    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


  6. #6
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372

    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!


  7. #7
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    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
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  9. #9

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    Talking

    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!!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width