Sorting out alphabetically via VBScript with dictionary.

This article contains a demo application that illustrates how to sort out items alphabetically via VBScript with the ‘dictionary‘ object and the SortDictionary function, as well as the script responsible for this execution.

Below is the code for the demo application at the end of the article:

'######################### Order Dictionary ##########################
Function SortDictionary(objDict,intSort)
Dim strDict()
Dim objKey
Dim strKey,strItem
Dim X,Y,Z

Z = objDict.Count

Const dictKey = 1
Const dictItem = 2

If Z > 1 Then
ReDim strDict(Z,2)
X = 0
For Each objKey In objDict
strDict(X,dictKey) = CStr(objKey)
strDict(X,dictItem) = CStr(objDict(objKey))
X = X + 1
Next

For X = 0 to (Z - 2)
For Y = X to (Z - 1)
If StrComp(strDict(X,intSort),strDict(Y,intSort),vbTextCompare) > 0 Then
strKey = strDict(X,dictKey)
strItem = strDict(X,dictItem)
strDict(X,dictKey) = strDict(Y,dictKey)
strDict(X,dictItem) = strDict(Y,dictItem)
strDict(Y,dictKey) = strKey
strDict(Y,dictItem) = strItem
End If
Next
Next

objDict.RemoveAll

For X = 0 to (Z - 1)
objDict.Add strDict(X,dictKey), strDict(X,dictItem)
Next
Set SortDictionary = objDict
End If
End Function

Related articles


Attachments:

SortingAlgorithm.zip

 

Este artigo foi útil? Was this post helpful?
Yes0
No0

Leave a Reply

Your email address will not be published.Required fields are marked *