vb 利用数组排序

利用数组排序。按“生成随机数”按钮,在第一个文本框中生成10个10~99之间的随机数,然后按“按大小排序”按钮,将这10个数按从大到小的顺序排列放在第二个文本框中。
(1)两个文本框(Text)、两个按钮(Command)
(2)用比较法完成排序
(3)编辑代码就好

Private Sub Command1_Click()
Text1.Text = ""
Dim i As Integer
For i = 1 To 10
Randomize
Text1.Text = Text1.Text & IIf(Text1.Text = "", "", ",") & Int(Rnd(i) * 89) + 10
Next i
End Sub

Private Sub Command2_Click()
Text2.Text = ""
Dim tmpGrp() As String
tmpGrp = Split(Text1.Text, ",")
ResetOrder tmpGrp, 1
Dim i As Integer
For i = 1 To UBound(tmpGrp)

Text2.Text = Text2.Text & IIf(Text2.Text = "", "", ",") & tmpGrp(i)
Next i
End Sub

Function ResetOrder(ByRef tmpGrp() As String, ByVal sIndex As Integer) As String
'对所有数字排序
Dim i, j As Integer
Dim tmpN As Integer
For i = UBound(tmpGrp) To sIndex Step -1
If Val(tmpGrp(i)) > Val(tmpGrp(i - 1)) Then
'前一个比后一个小,则互换
tmpN = tmpGrp(i)
tmpGrp(i) = tmpGrp(i - 1)
tmpGrp(i - 1) = tmpN
End If
Next i
If sIndex < UBound(tmpGrp) Then
ResetOrder tmpGrp, sIndex + 1
End If
End Function
温馨提示:答案为网友推荐,仅供参考
相似回答