如何排序数组

时间:2012-06-18 05:07:43

标签: vb6

使用VB6.0,我有一个包含字段“English”的学校数据库,我使用SQL创建记录集,并按描述顺序排列英文。我的问题是,我想对这些学生进行排名,以便在获得平局后,下一批学生获得排名后的排名如下:

Eng:45,48,67,67,67,80,80,91。

英语排名 91 - 1 80 - 2 80 - 2, 67 - 4, 67 - 4, 67 - 4, 48 - 7, 45 - 8,

2 个答案:

答案 0 :(得分:2)

你的问题不是很清楚,但我想你想要这样的东西?

select Eng, rank() over (order by Eng desc) EnglishRank from somewhere

答案 1 :(得分:0)

我想你的意思是以下代码。我只使用一种方法从程序中获取数据。你可能会做一些不同的事情。

Option Explicit

Private Type RankedScores
    Rank                As Long
    Score               As Long
End Type

Private Sub RankValues(ByRef the_rs As ADODB.Recordset, ByRef out_auRankedScores() As RankedScores)

    Dim nIndex                                  As Long
    Dim nRank                                   As Long
    Dim nScore                                  As Long
    Dim nLastScore                              As Long

    nRank = 0
    nIndex = 0

    ' Resize the output buffer to its maximum size (won't work on some types of recordsets).
    ReDim out_auRankedScores(1 To the_rs.RecordCount)

    Do Until the_rs.EOF

        nIndex = nIndex + 1

        ' Pull score out of the recordset. If it is not the same as the last score, then increment the rank.
        nScore = CLng(the_rs.Fields.Item("English"))
        If nScore <> nLastScore Then
            nRank = nIndex
        End If

        ' Write into output buffer.
        With out_auRankedScores(nIndex)
            .Rank = nRank
            .Score = nScore
        End With

        ' Reset last score.
        nLastScore = nScore

        ' Next row.
        the_rs.MoveNext
    Loop

End Sub
相关问题