Link to home
Start Free TrialLog in
Avatar of benlowry
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
ASKER CERTIFIED SOLUTION
Avatar of rbagdonas
rbagdonas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of benlowry
benlowry

ASKER

Thanks!  Appreciate the quick response too!
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("Scripting.Dictionary")

  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(iTemp))
  Next

  set SortCart = nothing

End Sub
'=======================================================
Sub SortOrder(objDict)
  Dim aTemp
  Call BuildArray(objDict, aTemp)
  Call SortArray(aTemp)
  Call ReBuildArray(objDict, aTemp)
End Sub
'=======================================================
%>
ahhahaha this was my question

i'm so tired :(
ROFL