我没有学过VB,请教个排序问题,我把4个值赋给了a(0),a(1),a(2),a(3)然后把排序后的值再分别赋给b(0),b(1),b(2),b(3),然后把b(0),b(1),b(2),b(3),按顺序显示出来,谢谢了,这是一部分代码:
Private Sub cmdOk_Click()
Dim a(3) As Long, b(3) As Long
Dim sR As String
a(0) = 5
a(1) = 1
a(2) = 7
a(3) = 6
'****ÅÅÐò
Dim j As Long
For j = 0 To 3
b(j) = CStr(j)
Next j
'****
Dim i As Long
sR = ""
For i = 0 To 3
sR = sR & CStr(b(i)) & ","
Next i
txtInfo.Text = sR
End Sub
參考答案:Private Sub cmdOk_Click()
Dim a(3) As Long, b(3) As Long
Dim sR As String
a(0) = 5
a(1) = 1
a(2) = 7
a(3) = 6
Dim j As Long
For j = 0 To 3
b(j) = a(j)
Next j
Dim k As Long
Dim Temp As Long
For j = 0 To 3
For k = 0 To 3
If (b(j) <= b(k)) Then
Temp = b(k)
b(k) = b(j)
b(j) = Temp
End If
Next k
Next j
Dim i As Long
sR = ""
For i = 0 To 3
sR = sR & CStr(b(i)) & ","
Next i
txtInfo.Text = sR
End Sub
这个是按升序排的 如果是降序 你可以把“<=”改为“>=”