dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Sorting String Array ...

  1. #1
    Join Date
    Apr 2002
    Location
    Los Angeles, Ca
    Posts
    238

    Sorting String Array ...

    Hi everyone ... In a certain portion of a project I need to sort a 15 element String Array. So far, all my tries have ended in compile errors :-(
    The only way I can think of do it now, would be to assign each element to a Hidden listbox whose sorted property is set to True, then re-assign the listBox listItems to each element of the Array, but I know there must be a better way!
    Any ideas?
    Thanks
    Michael

  2. #2
    Join Date
    Nov 2002
    Posts
    278
    I donít see anything wrong with your idea of letting a listbox sort the data for you. The Array variable certainly isnít going to sort itself. Your idea is creative improvising in my opinion.

  3. #3
    Join Date
    Oct 2001
    Location
    Melbourne, Australia
    Posts
    576
    heres some code I have used.
    Code:
    Public Function bubbleSortArray(ByVal inArray As Variant, ByVal elements As Integer) As Variant
        Dim i As Integer, ii As Integer
        Dim h As Variant, newArray() As Variant
        ReDim newArray(elements)
        newArray = inArray
        For i = elements - 1 To 1 Step -1
            For ii = 0 To i - 1
                If newArray(ii) > newArray(ii + 1) Then
                    h = newArray(ii)
                    newArray(ii) = newArray(ii + 1)
                    newArray(ii + 1) = h
                End If
            Next
        Next
        bubbleSortArray = newArray
    End Function
    and to use it just:

    Code:
    dim a(5) as string, i(5) as string
    
    i = array("1","3","4","2","5")
    a =bubbleSortArray(i, 5)
    there are quicker methods than a bubble sort, but for small arrays, this is more than adequate.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)