2017-08-30 84 views
1

分类收集下面我有VBA的价值

enter image description here

enter image description here

的VBA集合我想值进行排序,使得集合将最终在最高的双重价值最高索引位置(即,值为14的“e”在第一索引中,“c”在值10中是第二等)。这怎么可能?

Public Function SortCollection(ByVal c As Collection) As Collection 
    Dim n As Long: n = c.Count 

    If n = 0 Then 
     Set SortCollection = New Collection 
    Exit Function 

    ReDim Index(0 To n - 1) As Long ' allocate index array 
    Dim i As Long, m As Long 

    For i = 0 To n - 1: Index(i) = i + 1: Next ' fill index array 

    For i = n \ 2 - 1 To 0 Step -1 ' generate ordered heap 
     Heapify c, Index, i, n 
    Next 

    For m = n To 2 Step -1 ' sort the index array 
     Exchange Index, 0, m - 1 ' move highest element to top 
     Heapify c, Index, 0, m - 1 
    Next 

    Dim c2 As New Collection 
    For i = 0 To n - 1 ' fill output collection 
     c2.Add c.item(Index(i)) 
    Next 

    Set SortCollection = c2 

    End Function 

Private Sub Heapify(ByVal c As Collection, Index() As Long, ByVal i1 As Long, ByVal n As Long) 
    ' Heap order rule: a[i] >= a[2*i+1] and a[i] >= a[2*i+2] 
    Dim nDiv2 As Long: nDiv2 = n \ 2 
    Dim i As Long: i = i1 
    Do While i < nDiv2 
     Dim k As Long: k = 2 * i + 1 
     If k + 1 < n Then 
     If c.item(Index(k)) < c.item(Index(k + 1)) Then k = k + 1 
     End If 
     If c.item(Index(i)) >= c.item(Index(k)) Then Exit Do 
     Exchange Index, i, k 
     i = k 
     Loop 
    End Sub 

Private Sub Exchange(Index() As Long, ByVal i As Long, ByVal j As Long) 
    Dim Temp As Long: Temp = Index(i) 
    Index(i) = Index(j) 
    Index(j) = Temp 
    End Sub 
+0

你可以发布你的脚本。 – ShanayL

+0

不是,我大约有1500行,所有的数据都来自不同的角度。你需要更多的信息? – Iorek

+0

您可以添加创建集合的部分并尝试获取最高编号。 – ShanayL

回答

0

按照Domenic在评论和他没有回答。

“如果你使用一个Dictionary对象,而不是一个集合的,你可以通过值,如图here排序 - 在22:29 Domenic 8月30日”

这就是现在的工作。