2016-04-20 45 views
1

兩個價值在我的計劃,我想基於兩個值(得分和以前的排名)的球員我已經管理排序的球員進行排序..如何排序依據就在那裏得分基於在VBScript

我會喜歡重新排序,如果一個或多個玩傢俱有相同的得分,但具有最低的先前等級的玩家應該首先顯示。

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 
+1

嘗試通過'排序100000 * mPlayer(i).score±mPlayer(i).lowestRank'而不是裸mPlayer(i).score' where subst以最大'分數'值評價'100000'。然而,看看[排序算法](http://rosettacode.org/wiki/Category:Sorting_Algorithms)... – JosefZ

+0

沒有最高分。我會嘗試mPlayer(i).score±mPlayer(i).lowestRank – Decoder94

回答

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