在我的程序中,我想基于两个值(得分和之前的排名)对玩家进行排序,我已经设法根据得分进行排序..
我想重新排序,如果一个或多个玩家得分相同,但先前排名最低的玩家应先显示。
dim mPlayer(30)
Class Player
dim score
dim name
dim lowestRank
End Class
dim sorted, i
dim tempPlayer
Set tempPlayer = New Player
sorted = False
Do Until sorted
sorted = True
For i = 0 To mNumPlayers - 2
If (mPlayer(i + 1).score > mPlayer(i).score) Then
' swap
sorted = False
Call CopytempPlayerData(tempPlayer, mPlayer(i))
Call CopytempPlayerData(mPlayer(i), mPlayer(i + 1))
Call CopytempPlayerData(mPlayer(i + 1), tempPlayer)
End If
Next
Loop
Set temptempPlayer = Nothing
lstPlayers.Clear()
For i = 0 To mPlayer - 1
lstPlayers.Items.add(mPlayer(i).name)
Next
答案 0 :(得分:0)
阅读If...Then...Else Statement文档。语法:
' Block syntax: If condition Then [statements] [ElseIf condition-n Then [elseifstatements]] . . . [Else [elsestatements]] End If Single-Line syntax: If condition Then statements [Else elsestatements ]
适用于您的情况如下:
If (mPlayer(i + 1).score > mPlayer(i).score) Then
' swap
sorted = False
Call CopytempPlayerData(tempPlayer, mPlayer(i))
Call CopytempPlayerData(mPlayer(i), mPlayer(i + 1))
Call CopytempPlayerData(mPlayer(i + 1), tempPlayer)
Else If (mPlayer(i + 1).score = mPlayer(i).score) Then
''' not sure about Comparison Operator in next condition
If (mPlayer(i + 1).lowestRank < mPlayer(i).lowestRank) Then
' swap
sorted = False
Call CopytempPlayerData(tempPlayer, mPlayer(i))
Call CopytempPlayerData(mPlayer(i), mPlayer(i + 1))
Call CopytempPlayerData(mPlayer(i + 1), tempPlayer)
End If
End If
或使用Sub
procedure如下(记住Local Variables in Sub Procedures):
If (mPlayer(i + 1).score > mPlayer(i).score) Then
swapPlayers
Else If (mPlayer(i + 1).score = mPlayer(i).score) Then
''' not sure about Comparison Operator in next condition
If (mPlayer(i + 1).lowestRank < mPlayer(i).lowestRank) Then
swapPlayers
End If
End If
' remaining script statements here
Sub swapPlayers
' swap
sorted = False
Call CopytempPlayerData(tempPlayer, mPlayer(i))
Call CopytempPlayerData(mPlayer(i), mPlayer(i + 1))
Call CopytempPlayerData(mPlayer(i + 1), tempPlayer)
End Sub