我有一个文件source.txt
里面有500行数值数据:类如下:
02900000186
02900000982
02900001335
02900001570
... ...
02900017816
我现在想把它们排列一下:从小到大排列。
可是还没什么思路,请大虾指教,如有源代码让我瞧瞧,感激不尽!
多谢大虾指教!!
里面有500行数值数据:类如下:
02900000186
02900000982
02900001335
02900001570
... ...
02900017816
我现在想把它们排列一下:从小到大排列。
可是还没什么思路,请大虾指教,如有源代码让我瞧瞧,感激不尽!
多谢大虾指教!!
解决方案 »
- 请教一个VB调用医保链接库的问题?代码如下
- vb对话框之间的值传递
- 请问怎样怎样用vb显示outlook的地址簿??
- Adress in used --------------------- 着急!!!
- 请给我一个标准答案(关于dim,private,public),谢谢!
- 特急求助!(外地出差急用)vb+windows98/2000下怎么设置键盘鼠标钩子?各位,不管会不会帮个场也好啊!
- txt读取数据问题
- 关于.PDF格式的文件的问题?
- 新手的问题:为什么总提示未找到方法或数据成员?
- 请教懂得VB编程的朋友
- 再次新开一贴讨论透明问题,欢迎大家踊跃参与
- 发个关于使用inet控件和winsock控件的问题
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
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没有调试,应该没问题。
但是有个问题,如果是文本排序的话,会出现这种情况
1
10
2
3
30
4
但如果用数据排序的话,那么所有前面的0就会自动去掉
不知你想怎么做啊,我可以先给你写一段,很简单的。
當然是文本排序了,沒看見前面有0啊。如果是數字,那排序的方法多了,
哪本基礎的書上沒有啊。問題是在做項目的時候,要考慮開發時間和開發速度,
只要能達到目的,當然用最快的方法了。這又是考試,非要指定用哪種方法寫出答案。