求教一个VB排序问题:

一个变量的所有项中的每一项里含有序号和小数,现在要以小数从大到小排列
如:18 0.333 变为:16 0.400
11 0.400
17 0.250 13 0.375
16 0.400 18 0.333
15 0.333 15 0.333
14 0.286 12 0.333
13 0.375 14 0.286
12 0.333 17 0.250
11 0.400
请给出代码,谢谢!

Private Sub Command1_Click()

Dim a(1 To 8) As String
Dim i As Long, j As Long
Dim tmp As String

a(1) = "18 0.333"
a(2) = "17 0.250"
a(3) = "16 0.400"
a(4) = "15 0.333"
a(5) = "14 0.280"
a(6) = "13 0.375"
a(7) = "12 0.333"
a(8) = "11 0.400"

For i = 1 To 7
For j = i + 1 To 8
If GetValue(a(i)) < GetValue(a(j)) Then
tmp = a(i)
a(i) = a(j)
a(j) = tmp
End If
Next
Next

Debug.Print Join(a, vbCrLf)

End Sub

Function GetValue(ByVal s As String) As Double
GetValue = Val(Split(s, Chr(32))(1))
End Function

如果你排序的数量巨大,这个冒泡算法效率太低了,可以考虑用快排或借用recordset对象的sort方法...
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-06-30
Private Type ZS_XS
zs As Integer
xs As Single
End Type

Private Sub Form_Click()
Dim b(1 To 8) As ZS_XS, t As ZS_XS
Dim a(1 To 8) As String, s
a(1) = "18 0.333"
a(2) = "17 0.250"
a(3) = "16 0.400"
a(4) = "15 0.333"
a(5) = "14 0.280"
a(6) = "13 0.375"
a(7) = "12 0.333"
a(8) = "11 0.400"

For i = 1 To 8
s = Split(a(i), " ") '注意是整数和小数之间的分隔符
b(i).zs = Val(s(0))
b(i).xs = Val(s(1))
Next

For i = 1 To 7
For j = i + 1 To 8
If b(i).xs < b(j).xs Then
t = b(i): b(i) = b(j): b(j) = t
End If
Next
Next

For i = 1 To 8
Print b(i).zs; Format(b(i).xs, "0.000")
Next
End Sub
第2个回答  2009-06-30
快速排序法:

Private Sub Command1_Click()
Dim a() As Single
ReDim a(11 To 18)
Dim i As Long
a(18) = 0.333
a(17) = 0.25
a(16) = 0.4
a(15) = 0.333
a(14) = 0.28
a(13) = 0.375
a(12) = 0.333
a(11) = 0.4
QuickSort2 a(), LBound(a), UBound(a)
For i = LBound(a) To UBound(a) '显示排序结果
Print i & " " & Format(a(i), "0.000")
Next
End Sub

Public Sub QuickSort2(ByRef aStrSort() As Single, ByVal lngleft As Long, ByVal lngright As Long)
Dim i As Long
Dim j As Long
Dim temp As Long
i = lngleft
j = lngright
temp = aStrSort(i)

NextStep:
Do Until i >= j
While (aStrSort(j) > temp And j > i)
j = j - 1
Wend
If j > i Then
aStrSort(i) = aStrSort(j)
aStrSort(j) = temp
i = i + 1
End If
While (aStrSort(i) < temp And j > i)
i = i + 1
Wend
If j > i Then
aStrSort(j) = aStrSort(i)
aStrSort(i) = temp
j = j - 1
End If
Loop
If lngleft < i - 1 Then QuickSort2 aStrSort, lngleft, i - 1
If lngright > i + 1 Then QuickSort2 aStrSort, i + 1, lngright
End Sub

vb排序问题
你可以少拿几组数比如4组在纸上按程序执行写一下就理解了,光是别人说的可能不是很清楚。其实这两个解答都不是标准的解答,因为效率很低,还是按照教材上面的几种排序算法来。另外,VB中数组下标默认是从0开始的,上面都没有注意这一点。

求教一个VB排序问题:
Function GetValue(ByVal s As String) As Double GetValue = Val(Split(s, Chr(32))(1))End Function 如果你排序的数量巨大,这个冒泡算法效率太低了,可以考虑用快排或借用recordset对象的sort方法...

vb排序问题
Adodc1.RecordSource = "select * from 通讯录 where 类别 = '" & aa & "'and 性别 = '" & cc & "'Order By " & bb & "desc"

VB 排序问题
用冒泡法排序,是最基本的算法,你现在这个问题,代码如下 Dim a(0 to 2) as integer'定义一个数组存储需要比较的数字 Dim temp As Integer a(0) = Val(Text1.Text)a(1) = Val(Text2.Text)a(2) = Val(Text3.Text)For i = 0 To 2 For j = i To 2 If a(i) < a(j) Then ...

VB排序问题
If r(j) > r(j + 1) Then temp = r(j + 1)r(j + 1) = r(j)r(j) = temp End If Next j Next i Picture1.Print Picture1.Print "排序后:"For i = 1 To 10 Picture1.Print r(i);Next i Picture1.Print End Sub 托一个窗体 价格按钮和一个picture控件就可以运行了 ...

vb编程,任意输入3个整数,按照由小到大排序输出.(代码纠错)
a = Text1.Text ,这时候VB自动认为你定义了a为字符串变量。所以,当你进行声明 Dim a, b, c As Integer 的时候,就发生了“当前范围内的声明重复”,因为a已经隐式声明过了。b、c也一样。解决办法就是:将声明语句放在变量使用之前。改成这样:Dim a, b, c As Integer a = Text1.Text ...

vb问题 排序问题
1、Text2.Text = Text2.Text & Str(a(i))的位置不正确。2、交换变量位置的代码不对。3、可能缺乏数组a(1 To 20)的定义。程序修改如下:Private Sub Command1_Click()Dim a(1 To 20) As Integer Randomize For i = 1 To 20 a(i) = Int(99 * Rnd + 1)Text1.Text = Text1.Text...

VB排序问题
n = n + 1 ReDim Preserve b(n)b(n) = a(i)End If Next i End Sub Private Sub Command2_Click()List2.Clear For i = 1 To n - 1 '我惯用选择排序法 k = i For j = i + 1 To n If b(j) < b(k) Then k = j Next j t = b(i)b(i) = b(k)b(k) = t ...

求教VB高手解决排序问题
Private Sub Command1_Click()Dim a(1 To 10) As Integer For i = 1 To 10 a(i) = Int(Rnd * 90 + 10)Print a(i);Next i Print For i = 10 To 1 Step -1 For j = 1 To i - 1 If a(j) > a(j + 1) Then t = a(j)a(j) = a(j + 1)a(j + 1) = t ...

vb中关于排序问题
成绩是一个数组,他是字符类型string,而成绩是数值类型,当然要定义为两个数组啦 例如dim name(100) as string dim score(100) as integer

相似回答
大家正在搜