Results 1 to 11 of 11

Thread: Sorting array

  1. #1
    duckax
    Guest

    Sorting array

    What is the simplest way to sort items in an array in alphabetical order?

  2. #2
    jim mcnamara
    Guest
    With a ListBox List1.Sorted=True List1.Visible = False
    Code:
    Dim i,j
    
       For i = 0 to Ubound(arr)
             List1.AddItem arr(i)
       next 
       for i = 0 to List1.ListCount - 1
            arr(i) = List1.List(i)
       next
    Works for up to 32767 elements.

  3. #3
    That's simple but slow with larger (>1000) arrays. There's bubble sort, selection sort, the all-powerful and speedy merge sort.... But if you only have a few dozen elements, then the linear sort (what Jim suggested) is fine.

  4. #4
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    FL
    Posts
    258
    For smaller arrays here is a fairly simple way

    Code:
    Dim arrNames(5) As String
    
    Private Sub Command1_Click()
        For i = 0 To 4
            MsgBox "Names before sort....  " & vbCrLf & arrNames(i)
        Next i
        
        SortStringArray arrNames(), True, False
        
        For i = 0 To 4
            MsgBox "Names after sort....  " & vbCrLf & arrNames(i)
        Next i
    End Sub
    
    Private Sub Form_Load()
        arrNames(0) = "Steven"
        arrNames(1) = "John"
        arrNames(2) = "Mark"
        arrNames(3) = "Rob"
        arrNames(4) = "Chuck"
    End Sub
    Public Sub SortStringArray(MyArray() As String, _
                                      Optional ByVal bAscending As Boolean = True, _
                                      Optional ByVal bCaseSensitive As Boolean = False)
    
        Dim lMin            As Long
        Dim lMax            As Long
        Dim lOrder          As Long
        Dim lCompareType    As Long
    
        lMin = LBound(MyArray)
        lMax = UBound(MyArray) - 1
        
        If lMin = lMax Then
            Exit Sub
        End If
        
        ' Set Asc or Dsc Order
        lOrder = IIf(bAscending, 1, -1)
        
        ' Set Case Sensitive
        lCompareType = IIf(bCaseSensitive, vbBinaryCompare, vbTextCompare)
        
        Dim sValue  As String
        Dim lCnt1 As Long
        Dim lCnt2 As Long
        
        ' Loop through array move elements down to their correct place
        For lCount1 = lMin + 1 To lMax
            sValue = MyArray(lCount1)
            
            ' Find the place to put it
            For lCount2 = lCount1 - 1 To lMin Step -1
                If StrComp(MyArray(lCount2), sValue, lCompareType) <> lOrder Then
                    Exit For
                End If
                MyArray(lCount2 + 1) = MyArray(lCount2)
            Next lCount2
            
            ' Insert it
            MyArray(lCount2 + 1) = sValue
        Next
        
        End Sub
    Of course for large arrays you would want something faster.

  5. #5
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    what ever happened to the best there ever was and still is....quick sort?

  6. #6
    I think I've heard of it, but that it is only really speedy the larger the array. Can you give us a pseudocode algorithm for how it works again?

  7. #7
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    FL
    Posts
    258
    Here is an example of Quick Sort, courtesy of the VB2TheMax site

    http://www.vb2themax.com/Item.asp?PageID=CodeBank&ID=77
    Last edited by mdake; Jul 13th, 2001 at 08:56 AM.

  8. #8
    FYI, you can't click on his link, but mine works http://www.vb2themax.com/Item.asp?PageID=CodeBank&ID=77

  9. #9
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    FL
    Posts
    258
    Here is an excellent article from one of the true masters of VB,
    Francesco Balena
    It shows many different sorting methods.

    http://www.vb2themax.com/HtmlDoc.asp...Articles&ID=10

    I could click on the link in previous mention from myself?????
    Last edited by mdake; Jul 13th, 2001 at 09:00 AM.

  10. #10
    This last one is screwed up too. It tries to open http:/// which obviously doesn't exist. If you're using the [ url ] and [ /url ] tags, don't.

  11. #11
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    FL
    Posts
    258
    Oops, sorry
    I pasted the link into the Text part, my mistake

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