Search code examples
sortingdictionaryvbscript

How to sort dictionary by object property vbscript


I am trying to sort a dictionary with a function that I found online by an object property which is the Id but on this For Each i In dict line I am getting this error message Microsoft VBScript runtime error: Object doesn't support this property or method. I have tried For Each i In dict.Items but I get the same error message with 'dict.Items' I am using a older version of VBScript so it does not have features like dict.Count

VBScript Class:

Class TestClass
    Public ID
    Public TestText
    Private Sub Class_Initialize
            TestText  = ""
    End Sub
End Class

Set gDic = CreateObject("Scripting.Dictionary")


For i = 1 to 5
    Set temp = new TestClass
    temp.ID = i
    temp.TestText = "Test" & i

    gDic.Add i,temp
Next


Set NewDic = SortDict(gDic)
msgbox NewDic.Items()(1).TestText

Sort function:

Function SortDict(ByVal dict)
    Dim i, j, temp
    For Each i In dict
        For Each j In dict
            If(dict.Item(i) <= dict.Item(j)) Then
                temp = dict.Item(i)
                dict.Item(i) = dict.Item(j)
                dict.Item(j) = temp
            End If
        Next
    Next
    Set SortDict = dict
End Function

Solution

  • Try modifying your function to:

    Function SortDict(dict)
        Dim i, j, arrKeys, arrItems
        arrKeys = dict.keys                                               'Array containing the keys
        arrItems = dict.Items                                             'Array containing the Items(which are nothing but objects of class TestClass)
        Set tempObj = New TestClass
        For i=0 To UBound(arrItems)-1                                     'From 1st element to the penultimate element
            For j=i+1 To UBound(arrItems)                                 'From i+1th element to last element
                If arrItems(i).id < arrItems(j).id Then                  'Sorting in DESCENDING ORDER by the Property "ID"
                    tempObj.ID = arrItems(i).ID
                    tempObj.TestText = arrItems(i).testText
                    dict.item(arrKeys(i)).ID = arrItems(j).ID
                    dict.item(arrKeys(i)).TestText = arrItems(j).TestText
                    dict.item(arrKeys(j)).ID = tempObj.ID
                    dict.item(arrKeys(j)).TestText = tempObj.TestText
                End If
            Next
        Next
        Set SortDict = dict
    End Function
    

    Before Sorting:

    |Key             |Value                |
    |----------------|---------------------|
    |1               |1,Test1              |
    |2               |2,Test2              |
    |3               |3,Test3              |
    |4               |4,Test4              |
    |5               |5,Test5              |
    

    After Sorting:

    |Key             |Value                |
    |----------------|---------------------|
    |1               |5,Test5              |
    |2               |4,Test4              |
    |3               |3,Test3              |
    |4               |2,Test2              |
    |5               |1,Test1              |
    

    I could not find a better way to swap the values. I am sure there is a better way to do that. Will update it once I get something.