Results 1 to 4 of 4

Thread: HELP! Sorting Numbers From Text File!

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    2

    HELP! Sorting Numbers From Text File!

    ok so there are 300 numbers in a text file, they are all out of order and im just trying to print all of them in order from smallest to biggest

    i really need some help, please dont just direct me to another topic about this unless its exactly like the help i need

    this is teh code i have so far, but it wont work

    thank you so much, hopefully someone knows whats wrong

    VB Code:
    1. Dim nom(1 To 300)
    2. Private Sub Form_Load()
    3. Dim count As Single
    4. Open "NUMBERS.TXT" For Input As #1
    5. Do While EOF(1) = False
    6.     count = count + 1
    7.     Input #1, nom(count)
    8. Loop
    9. Close #1
    10. End Sub
    11.  
    12. Private Sub cmdCalculate_click()
    13. Call Sort
    14. Call Show
    15. End Sub
    16.  
    17. Private Sub Showz()
    18. Dim i As Integer
    19.  
    20. picOutput.Cls
    21. For i = 1 To 300
    22.     picOutput.Print nom(count)
    23. Next i
    24. End Sub
    25. Private Sub Sort()
    26. Dim passNum As Single, count2 As Single
    27. 'Bubble sort table in descending order by population
    28. For passNum = 1 To 299
    29.     For count2 = 1 To 300 - passNum
    30.             If nom(count2) > nom(count2 + 1) Then
    31.             temp = nom(count2)
    32.             nom(count2) = nom(count2 + 1)
    33.             nom(count2 + 1) = temp
    34.             End If
    35.             Next count2
    36.     Next passNum
    37. End Sub

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    2

    Re: HELP! Sorting Numbers From Text File!

    double post, i came up with some new code but it prints the first number everytime instead of going to the next

    cant any of you guys just look at my code for one sec and try to figure something out

    VB Code:
    1. Dim nom(1 To 500) As Single
    2. Private Sub cmdDumb_Click()
    3. Dim count As Single, flag As Boolean
    4. Open App.Path & "\7Data.Txt" For Input As #1
    5. For i = 1 To 100
    6.     Input #1, nom(i)
    7. Do
    8. count = 1
    9. flag = 0
    10. If nom(count) > nom(count + 1) Then
    11. temp = nom(count)
    12. nom(count) = nom(count + 1)
    13. nom(count + 1) = temp
    14. flag = 1
    15. End If
    16. picOutput.Print temp
    17. count = count + 1
    18. Loop Until flag = 0
    19. Next i
    20. Close #1
    21. End Sub

  3. #3
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: HELP! Sorting Numbers From Text File!

    Quote Originally Posted by kapaka2
    cant any of you guys just look at my code for one sec and try to figure something out

    VB Code:
    1. Dim nom(1 To 500) As Single
    2. Private Sub cmdDumb_Click()
    3. Dim count As Single, flag As Boolean
    4. Open App.Path & "\7Data.Txt" For Input As #1
    5. For i = 1 To 100
    6.     [COLOR=Red]Input #1, nom(i)[/COLOR]
    7. Do
    8. count = 1
    9. flag = 0
    10. If nom(count) > nom(count + 1) Then
    11. temp = nom(count)
    12. nom(count) = nom(count + 1)
    13. nom(count + 1) = temp
    14. flag = 1
    15. End If
    16. picOutput.Print temp
    17. count = count + 1
    18. Loop Until flag = 0
    19. Next i
    20. Close #1
    21. End Sub
    Try using Line Input instead of just Input.

    Also, a quicker way, if the file isn't too big, is to load the entire file at once into an array, and then sorting that array.

    Here's an example:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim lonFF As Long, strData() As String
    5.     Dim lonLoop As Long
    6.    
    7.     'Get an available file handle.
    8.     lonFF = FreeFile
    9.    
    10.     Open "C:\numbers.txt" For Input As #lonFF
    11.         'Load the entire contents of the file into strData variable
    12.         'while splitting it into an array.
    13.         strData() = Split(Input(LOF(lonFF), lonFF), vbNewLine)
    14.     Close #lonFF
    15.    
    16.     'Sort the array.
    17.     ShellSortNumbers strData()
    18.    
    19.     lonFF = FreeFile
    20.    
    21.     'Open the output file.
    22.     Open "C:\numbers (sorted).txt" For Append As #lonFF
    23.        
    24.         'Loop through sorted array and print to file.
    25.         For lonLoop = 0 To UBound(strData())
    26.             Print #lonFF, strData(lonLoop)
    27.         Next lonLoop
    28.        
    29.     Close #lonFF
    30.    
    31. End Sub
    32.  
    33. 'MSDN's version of sorting numbers (SLOW).
    34. 'Replace with your own.
    35. Sub ShellSortNumbers(vArray As Variant)
    36.   Dim lLoop1 As Long
    37.   Dim lHold As Long
    38.   Dim lHValue As Long
    39.   Dim lTemp As Long
    40.  
    41.   'startTime = Time()
    42.   lHValue = LBound(vArray)
    43.   Do
    44.     lHValue = 3 * lHValue + 1
    45.   Loop Until lHValue > UBound(vArray)
    46.   Do
    47.     lHValue = lHValue / 3
    48.     For lLoop1 = lHValue + LBound(vArray) To UBound(vArray)
    49.       lTemp = vArray(lLoop1)
    50.       lHold = lLoop1
    51.       Do While vArray(lHold - lHValue) > lTemp
    52.         vArray(lHold) = vArray(lHold - lHValue)
    53.         lHold = lHold - lHValue
    54.         If lHold < lHValue Then Exit Do
    55.       Loop
    56.       vArray(lHold) = lTemp
    57.     Next lLoop1
    58.   Loop Until lHValue = LBound(vArray)
    59.   'endTime = Time()
    60.   'timeToSort = endTime - startTime
    61.   'timeTaken = timeTaken & ";   Shell Sort: " & timeToSort
    62. End Sub

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: HELP! Sorting Numbers From Text File!

    Try using Line Input instead of just Input.
    who said the file had lines?

    do a search on this forum for quick sort or bubble sort
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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