我有一个文件source.txt
里面有500行数值数据:类如下:
02900000186
02900000982
02900001335
02900001570
... ...
02900017816
我现在想把它们排列一下:从小到大排列。
可是还没什么思路,请大虾指教,如有源代码让我瞧瞧,感激不尽!
多谢大虾指教!!

解决方案 »

  1.   

    建立Col1、Col2
        Dim Col1 As New Collection
        Dim Col2 As New Collection将文件按行添加到Col1中,循环检索Col1中的最小值,可以用StrComp比较,找出最小值,添加此最小值到Col2中,同时从Col1中移除此条目
    再重现循环检测Col1,直到Col1中的条目为零,Col2中的条目就是从小到大排列的Private Sub Form_Load()
        Dim fNum As Integer
        Dim Col1 As New Collection
        Dim Col2 As New Collection
        Dim FileName As String
        Dim lLen As Long
        Dim Index As Long
        Dim i As Long
        Dim str As String    FileName = "D:\1.txt"
        
        fNum = FreeFile
        Open FileName For Input As fNum
        Do While Not EOF(fNum)
            Line Input #fNum, str
            Col1.Add str
        Loop
        Close #fNum
        
        
        Do While Col1.Count > 0
            Index = 1
            str = Col1.Item(Index)
            For i = Index + 1 To Col1.Count
                If StrComp(Col1.Item(i), str, vbTextCompare) = -1 Then
                    str = Col1.Item(i)
                    Index = i
                End If
            Next i
            Col2.Add Col1.Item(Index)
            Col1.Remove Index
        Loop
        
        For i = 1 To Col2.Count
            Debug.Print Col2.Item(i)
        Next i
    End Sub
      

  2.   

    有必要自己算吗?用ListBox控件,一行行加进去,然后设置一下排序属性。一切OK了。
      

  3.   

    二分法快速排序:Private Sub Command1_Click()
    Sortit "c:\source.txt"
    End Sub
    Sub Sortit(ByVal afile As String)
    Dim min As Long, max As Long, num As Long, i As Long, j As Long, k As Long, temp As New Collection
    Dim b() As Byte, x() As String
    Open afile For Binary As #1
    ReDim b(LOF(1) - 1)
    Get #1, , b
    Close #1
    x = Split(StrConv(b, vbUnicode), vbCrLf)
    min = 0
    max = UBound(x)
    temp.Add x(min)
    For num = min + 1 To max
    i = 1
    j = temp.Count
    If x(num) < temp(i) Then temp.Add x(num), BEFORE:=i: GoTo over
    If x(num) > temp(j) Then temp.Add x(num), AFTER:=j: GoTo over
    Do While j > i + 1
    k = (i + j) \ 2
    If x(num) > temp(k) Then
    i = k
    Else
    j = k
    End If
    Loop
    temp.Add x(num), BEFORE:=j
    over:
    NextFor num = min To max
    x(num) = temp(num - min + 1)
    Next
    Set temp = NothingOpen afile For Binary As #1
    Put #1, , Join(x, vbCrLf)
    Close #1MsgBox "ok!"
    End Sub没有调试,应该没问题。
      

  4.   

    这个问题有个最简单的办法,就是读到LISTVIEW控件中,然后排序
    但是有个问题,如果是文本排序的话,会出现这种情况
    1
    10
    2
    3
    30
    4
    但如果用数据排序的话,那么所有前面的0就会自动去掉
    不知你想怎么做啊,我可以先给你写一段,很简单的。
      

  5.   

    liang80318(小亮) 
    當然是文本排序了,沒看見前面有0啊。如果是數字,那排序的方法多了,
    哪本基礎的書上沒有啊。問題是在做項目的時候,要考慮開發時間和開發速度,
    只要能達到目的,當然用最快的方法了。這又是考試,非要指定用哪種方法寫出答案。