Vb数字排序(N个数据不限,升序或者降序)编程
在编程中有很多时候我们需要对很多个数据进行排序或者分类,那么我们就需要对他们进行排序,在Exel、Word当中我们在图表操作当中也都用过降序排序或者升序排序,那到底是怎么编程的呢?方法不止一个!这里介绍下我自己通过思考编的一道程序:
步骤/方法
- 01
打开Vb6.0新建工程-标准EXE》在Form1代码模块输入如下代码: Private Sub Command1_Click() Dim Data() As String, MaxNum As Integer, Temp As Integer, StepVal As Integer, i As Integer, i2 As Integer Dim TempStr As String '降序排序程式 Data() = Split(Text1.Text, ",") StepVal = UBound(Data()) - 1 For i = 0 To StepVal For t = 0 To i ' If Val(Data(t)) > Val(Data(i + 1)) Then ' ' MaxNum = Data(t) '排序不变,取大数给MaxNum If Val(Data(t)) < Val(Data(i + 1)) Then MaxNum = Data(i + 1) '排序改变 Data(i + 1) = Data(t) '前后两个变量数据交换 Data(t) = MaxNum 'ElseIf Data(t) = Data(i + 1) Then '两个数据相同,不做前后排序 End If Next Next For i2 = 0 To UBound(Data()) TempStr = TempStr & IIf(TempStr = "", "", ",") & Data(i2) Next Text2.Text = TempStr MsgBox "Ok!" End Sub Private Sub Command2_Click() Dim Data() As String, MaxNum As Integer, Temp As Integer, StepVal As Integer, i As Integer, i2 As Integer '降序排序程式 Data() = Split(Text1.Text, ",") StepVal = UBound(Data()) - 1 For i = 0 To StepVal For t = 0 To i If Val(Data(t)) < Val(Data(i + 1)) Then MaxNum = Data(t) '排序不变,取大数给MaxNum ElseIf Val(Data(t)) > Val(Data(i + 1)) Then MaxNum = Data(i + 1) '排序改变 Data(i + 1) = Data(t) '前后两个变量数据交换 Data(t) = MaxNum ElseIf Data(t) = Data(i + 1) Then '两个数据相同,不做前后排序 End If Next Next Text2.Text = "" For i2 = 0 To UBound(Data()) Text2.Text = Text2.Text & "," & Data(i2) Next MsG.EndTime End Sub Private Sub Command3_Click() Dim i As Integer, s As Integer s = Val(Text3.Text) Text1.Text = "" For i = 0 To s Text1.Text = Text1.Text & IIf(Text1.Text = "", "", ",") & Int(Rnd * 1000) Next End Sub
- 02
窗体的外观设计:
- 03
好了,Vb排序程序演示到此Ok,有什么疑问或者建议请到我空间留言,Thanks you.