Results 1 to 4 of 4

Thread: What Am I Missing?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    2
    I'm trying to get the sorted list of 10 numbers to show up in txtSorted, but it's not happening. What am I doing wrong?

    Code:
    Private Sub cmdSort_Click()
        For pos = 1 To 9
            min = intArray(pos)
            minInd = pos
                For index = pos + 1 To 8
                    If intArray(index) < min Then
                        min = intArray(index)
                        minInd = index
                    End If
                Next index
            temp = intArray(pos)
            intArray(pos) = intArray(index)
            intArray(index) = temp
            txtSorted.Text = intArray(pos) & "     " & txtSorted.Text
         Next pos
         txtSorted.Text = intArray(10) & "     " & txtSorted.Text
    End Sub

  2. #2
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Try this:

    Code:
    --------------------------------------------------
    Option Explicit

    Dim intArray(9) As Integer
    Dim pos As Integer
    Dim index As Integer
    Dim min As Integer
    Dim intTemp As Integer


    Private Sub cmdSort_Click()

    For pos = 0 To 8
    min = intArray(pos)
    For index = pos + 1 To 9
    If intArray(index) < min Then
    intTemp = intArray(index)
    intArray(index) = min
    min = intTemp
    End If
    Next index
    intArray(pos) = min
    Next pos

    For pos = 0 To 9
    txtSorted.Text = txtSorted.Text & intArray(pos) & " "
    Next pos

    End Sub
    ------------------------------------------------
    Hope it helps.

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    or this way...
    Code:
    Private Sub Form_Load()
        Dim i As Integer
        Randomize
        Dim myArr(9)
        For i = 0 To 9
            myArr(i) = Fix(Rnd * 22)
        Next i
        
        Call mySort(myArr)
        
        Text1.Text = ""
        
        For i = LBound(myArr) To UBound(myArr)
            Text1 = Text1 & myArr(i) & vbNewLine
        Next i
    End Sub
    
    Sub mySort(iArray As Variant)
         
         Dim lLoop1 As Long
         Dim lLoop2 As Long
         Dim lTemp As Long
         
         For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
           For lLoop2 = LBound(iArray) + 1 To lLoop1
    
             If iArray(lLoop2 - 1) > iArray(lLoop2) Then
               lTemp = iArray(lLoop2 - 1)
               iArray(lLoop2 - 1) = iArray(lLoop2)
               iArray(lLoop2) = lTemp
             End If
           Next lLoop2
         Next lLoop1
       End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    HeSaidJoe's code is versatile. Well done.

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