tisdag 19 juni 2012

VBScript - How to sort an array of objects

Function SortItemsByPubDate(items, intSort)
    Dim result : Set result = Server.CreateObject("Scripting.Dictionary")
    Dim objDict()
    Dim objKey
    Dim strKey,strItem, objItem
    Dim X,Y,itemCount
    Const DICT_KEY = 1
    Const DICT_ITEM = 2
    Dim oRssItem

    itemCount = items.Count
    
        If itemCount > 1 Then
        
        ReDim objDict(itemCount,2)
        X = 0
        
        ' Add key and the value to sort the array on
        For Each objKey In items
            objDict(X,DICT_KEY)  = CStr(objKey)
            objDict(X,DICT_ITEM) = items(objKey).pubDate
            X = X + 1
        Next
        
        
        For X = 0 to (itemCount - 2)
            For Y = X to (itemCount - 1)
                If StrComp(objDict(X,intSort),objDict(Y,intSort),vbTextCompare) > 0 Then
                    strKey  = objDict(X,DICT_KEY)
                    strItem = objDict(X,DICT_ITEM)
                    objDict(X,DICT_KEY)  = objDict(Y,DICT_KEY)
                    objDict(X,DICT_ITEM) = objDict(Y,DICT_ITEM)
                    objDict(Y,DICT_KEY)  = strKey
                    objDict(Y,DICT_ITEM) = strItem
                End If
            Next
        Next
        
        
        For X = 0 to (itemCount - 1)
            result.Add objDict(X,DICT_KEY), objDict(X,DICT_ITEM)
        Next

        Set SortItemsByPubDate = result
    Else

        Set SortItemsByPubDate = items
    End If
        
End Function

Inga kommentarer:

Skicka en kommentar