01.
<%@LANGUAGE=
"VBSCRIPT"
%>
02.
<%
Option
Explicit %>
03.
<html>
04.
<head><title>Dictionary Sorting</title></head>
05.
<body>
06.
<%
07.
08.
Dim
d, i
09.
10.
Const
dictKey = 1
11.
Const
dictItem = 2
12.
13.
Set
d = Server.CreateObject(
"Scripting.Dictionary"
)
14.
15.
d.Add
"3"
,
"Delta"
16.
d.Add
"1"
,
"Foxtrot"
17.
d.Add
"4"
,
"Bravo"
18.
d.Add
"2"
,
"Echo"
19.
d.Add
"6"
,
"Alpha"
20.
d.Add
"5"
,
"Charlie"
21.
22.
Response.Write
"<p>Before Sorting:<br>"
23.
For
Each
i
In
d
24.
Response.Write i &
"="
& d(i) &
"<br>"
25.
Next
26.
27.
Response.Write
"<p>By Key:<br>"
28.
SortDictionary d,dictKey
29.
For
Each
i
In
d
30.
Response.Write i &
"="
& d(i) &
"<br>"
31.
Next
32.
33.
Response.Write
"<p>By Item:<br>"
34.
SortDictionary d,dictItem
35.
For
Each
i
In
d
36.
Response.Write d(i) &
"="
& i &
"<br>"
37.
Next
38.
%>
39.
</body>
40.
</html>
41.
<%
42.
Function
SortDictionary(objDict,intSort)
43.
Dim
strDict()
44.
Dim
objKey
45.
Dim
strKey,strItem
46.
Dim
X,Y,Z
47.
Z = objDict.Count
48.
If
Z > 1
Then
49.
ReDim
strDict(Z,2)
50.
X = 0
51.
For
Each
objKey
In
objDict
52.
strDict(X,dictKey) =
CStr
(objKey)
53.
strDict(X,dictItem) =
CStr
(objDict(objKey))
54.
X = X + 1
55.
Next
56.
For
X = 0 to (Z - 2)
57.
For
Y = X to (Z - 1)
58.
If
StrComp(strDict(X,intSort),strDict(Y,intSort),vbTextCompare) > 0
Then
59.
strKey = strDict(X,dictKey)
60.
strItem = strDict(X,dictItem)
61.
strDict(X,dictKey) = strDict(Y,dictKey)
62.
strDict(X,dictItem) = strDict(Y,dictItem)
63.
strDict(Y,dictKey) = strKey
64.
strDict(Y,dictItem) = strItem
65.
End
If
66.
Next
67.
Next
68.
objDict.RemoveAll
69.
For
X = 0 to (Z - 1)
70.
objDict.Add strDict(X,dictKey), strDict(X,dictItem)
71.
Next
72.
End
If
73.
End
Function
74.
%>