QuickSorting使用多个排序键的类集合,这可能吗?

时间:2018-06-07 16:22:17

标签: excel-vba vba excel

我修改了我在网上找到的QuickSort算法,它对Collection进行了排序。就在这里,https://excelmacromastery.com/excel-vba-collections/#Sorting_a_Collection,感谢作者。

我的这个版本可以使用类中包含的排序键对类集合进行排序。我想要做的是扩展此算法以通过第二个键对集合进行排序,该键也包含在类中。我怎么能这样做?我对排序键进行了硬编码,可能需要参数化。这是我的代码:

Sub ClassQuickSort(coll As Collection, first As Long, last As Long)

    Dim vCentreVal As Variant

    Dim lTempLow As Long
    Dim lTempHi As Long
    lTempLow = first
    lTempHi = last

    Dim clsTemporaryError As clsError, clsLowError As clsError, clsHiError As clsError
    Dim clsSingleError As clsError

    Set clsSingleError = coll.Item((first + last) \ 2)
    vCentreVal = clsSingleError.variable_name

    Do While lTempLow <= lTempHi

        Do While coll.Item(lTempLow).variable_name < vCentreVal And lTempLow < last
          lTempLow = lTempLow + 1
        Loop

        Do While vCentreVal < coll.Item(lTempHi).variable_name And lTempHi > first
          lTempHi = lTempHi - 1
        Loop

        If lTempLow <= lTempHi Then

          ' Swap values
          Set clsTemporaryError = coll.Item(lTempLow)

          coll.Add coll.Item(lTempHi), After:=lTempLow
          coll.Remove lTempLow

          coll.Add clsTemporaryError, Before:=lTempHi
          coll.Remove lTempHi + 1

          ' Move to next positions
          lTempLow = lTempLow + 1
          lTempHi = lTempHi - 1

        End If

  Loop

  If first < lTempHi Then ClassQuickSort coll, first, lTempHi
  If lTempLow < last Then ClassQuickSort coll, lTempLow, last

End Sub

clsError有6个成员,如下所示:

Public filename As String
Public SysID_Data As Integer
Public PlanReference_Data As String
Public MemberReference_Data As String
Public variable_name As String
Public errortype As String

0 个答案:

没有答案