benlowry
asked on
Sorting a Dictionary Object alphabetically??
Hi,
I'm in the final stages of an ecommerce site and I've noticed that when adding/removing from the order the products in the cart get shuffled around sometimes.
I've used a Dictionary Object to store the selected products in a session var. If i can get the products sorted alphabetically before being displayed, this will eliminate the shuffling around. Any suggestions?
Cheers,
Ben Lowry
I'm in the final stages of an ecommerce site and I've noticed that when adding/removing from the order the products in the cart get shuffled around sometimes.
I've used a Dictionary Object to store the selected products in a session var. If i can get the products sorted alphabetically before being displayed, this will eliminate the shuffling around. Any suggestions?
Cheers,
Ben Lowry
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have a sub I use to sort a dictionary object:
Call it using
SortOrder(dictionaryObject )
<%
'========================= ========== ========== ========== ==
Sub BuildArray(objDict, aTempArray)
Dim nCount, strKey
nCount = 0
'-- Redim the array to the number of keys we need
Redim aTempArray(objDict.Count - 1)
'-- Load the array
For Each strKey In objDict.Keys
'-- Set the array element to the key
aTempArray(nCount) = strKey
'-- Increment the count
nCount = nCount + 1
Next
End Sub
'========================= ========== ========== ==========
Sub SortArray(aTempArray)
Dim iTemp, jTemp, strTemp
For iTemp = 0 To UBound(aTempArray)
For jTemp = 0 To iTemp
If strComp(aTempArray(jTemp), aTempArray(iTemp)) > 0 Then
'Swap the array positions
strTemp = aTempArray(jTemp)
aTempArray(jTemp) = aTempArray(iTemp)
aTempArray(iTemp) = strTemp
End If
Next
Next
End Sub
'========================= ========== ========== ==========
Sub ReBuildArray(objDict, aTempArray)
Set SortCart = Server.CreateObject("Scrip ting.Dicti onary")
for each Prod in objDict
SortCart.add Prod, objDict(Prod)
next
objDict.RemoveAll
Dim iTemp
For iTemp = 0 To UBound(aTempArray)
objDict.add aTempArray(iTemp), SortCart.Item(aTempArray(i Temp))
Next
set SortCart = nothing
End Sub
'========================= ========== ========== ==========
Sub SortOrder(objDict)
Dim aTemp
Call BuildArray(objDict, aTemp)
Call SortArray(aTemp)
Call ReBuildArray(objDict, aTemp)
End Sub
'========================= ========== ========== ==========
%>
Call it using
SortOrder(dictionaryObject
<%
'=========================
Sub BuildArray(objDict, aTempArray)
Dim nCount, strKey
nCount = 0
'-- Redim the array to the number of keys we need
Redim aTempArray(objDict.Count - 1)
'-- Load the array
For Each strKey In objDict.Keys
'-- Set the array element to the key
aTempArray(nCount) = strKey
'-- Increment the count
nCount = nCount + 1
Next
End Sub
'=========================
Sub SortArray(aTempArray)
Dim iTemp, jTemp, strTemp
For iTemp = 0 To UBound(aTempArray)
For jTemp = 0 To iTemp
If strComp(aTempArray(jTemp),
'Swap the array positions
strTemp = aTempArray(jTemp)
aTempArray(jTemp) = aTempArray(iTemp)
aTempArray(iTemp) = strTemp
End If
Next
Next
End Sub
'=========================
Sub ReBuildArray(objDict, aTempArray)
Set SortCart = Server.CreateObject("Scrip
for each Prod in objDict
SortCart.add Prod, objDict(Prod)
next
objDict.RemoveAll
Dim iTemp
For iTemp = 0 To UBound(aTempArray)
objDict.add aTempArray(iTemp), SortCart.Item(aTempArray(i
Next
set SortCart = nothing
End Sub
'=========================
Sub SortOrder(objDict)
Dim aTemp
Call BuildArray(objDict, aTemp)
Call SortArray(aTemp)
Call ReBuildArray(objDict, aTemp)
End Sub
'=========================
%>
ASKER
ahhahaha this was my question
i'm so tired :(
i'm so tired :(
ROFL
ASKER