我正在寻找一种在VBA中对数组进行体面排序的实现。最好使用Quicksort。或除气泡或合并以外的任何其他sort algorithm都足够。

请注意,这是与MS Project 2003一起使用的,因此应避免使用任何Excel本机功能以及与.net相关的任何内容。

最佳答案

看看here:
编辑:引用的源(allexperts.com)此后已关闭,但是此处是相关的author注释:


Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)
  Dim pivot   As Variant
  Dim tmpSwap As Variant
  Dim tmpLow  As Long
  Dim tmpHi   As Long

  tmpLow = inLow
  tmpHi = inHi

  pivot = vArray((inLow + inHi) \ 2)

  While (tmpLow <= tmpHi)
     While (vArray(tmpLow) < pivot And tmpLow < inHi)
        tmpLow = tmpLow + 1
     Wend

     While (pivot < vArray(tmpHi) And tmpHi > inLow)
        tmpHi = tmpHi - 1
     Wend

     If (tmpLow <= tmpHi) Then
        tmpSwap = vArray(tmpLow)
        vArray(tmpLow) = vArray(tmpHi)
        vArray(tmpHi) = tmpSwap
        tmpLow = tmpLow + 1
        tmpHi = tmpHi - 1
     End If
  Wend

  If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
  If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi
End Sub

请注意,这仅适用于一维(又名“正常”?)数组。 (有一个有效的多维数组QuickSort here。)

关于arrays - VBA数组排序功能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/152319/

10-10 14:24
查看更多