Const dictKey = 1
Const dictItem = 2
Function SortDictionary(objDict,intSort)
' declare our variables
Dim strDict()
Dim objKey
Dim strKey,strItem
Dim X,Y,Z
' get the dictionary count
Z = objDict.Count
' we need more than one item to warrant sorting
If Z > 1 Then
' create an array to store dictionary information
ReDim strDict(Z,2)
X = 0
' populate the string array
For Each objKey In objDict
strDict(X,dictKey) = CStr(objKey)
strDict(X,dictItem) = CStr(objDict(objKey))
X = X + 1
Next
' perform a a shell sort of the string array
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
' erase the contents of the dictionary object
objDict.RemoveAll
' repopulate the dictionary with the sorted information
For X = 0 to (Z - 1)
objDict.Add strDict(X,dictKey), strDict(X,dictItem)
Next
End If
End Function
工作範例
下列範例會使用上述的函式。
附註:已從實際的排序函式在此範例中移除所有註解。
複製下列的 ASP 程式碼和指令儲存起來當作 Sortdemo.asp 與資料夾至少碼的存取:
<%@LANGUAGE="VBSCRIPT"%>
<% Option Explicit %>
<html>
<head><title>Dictionary Sorting</title></head>
<body>
<%
Dim d, i
Const dictKey = 1
Const dictItem = 2
Set d = Server.CreateObject("Scripting.Dictionary")
d.Add "3", "Delta"
d.Add "1", "Foxtrot"
d.Add "4", "Bravo"
d.Add "2", "Echo"
d.Add "6", "Alpha"
d.Add "5", "Charlie"
Response.Write "<p>Before Sorting:<br>"
For Each i In d
Response.Write i & "=" & d(i) & "<br>"
Next
Response.Write "<p>By Key:<br>"
SortDictionary d,dictKey
For Each i In d
Response.Write i & "=" & d(i) & "<br>"
Next
Response.Write "<p>By Item:<br>"
SortDictionary d,dictItem
For Each i In d
Response.Write d(i) & "=" & i & "<br>"
Next
%>
</body>
</html>
<%
Function SortDictionary(objDict,intSort)
Dim strDict()
Dim objKey
Dim strKey,strItem
Dim X,Y,Z
Z = objDict.Count
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
End If
End Function
%>
時瀏覽,應該顯示下列輸出:
Before Sorting:
3=Delta
1=Foxtrot
4=Bravo
2=Echo
6=Alpha
5=Charlie
By Key:
1=Foxtrot
2=Echo
3=Delta
4=Bravo
5=Charlie
6=Alpha
By Item:
Alpha=6
Bravo=4
Charlie=5
Delta=3
Echo=2
Foxtrot=1
Microsoft 僅,為了說明提供程式設計範例,不提供任何明示或默示的保證。這包括,但不限於適售性或適合某特定用途之默示擔保責任。本文假設您已熟悉使用我們所示範的程式設計語言以及建立和偵錯程序所使用的工具。Microsoft 技術支援工程師可以協助解釋特定程序的功能,但它們不會修改這些範例以提供附加功能或建構程序,以符合您特定需求。
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。