Sorting a Dictionary Object alphabetically??


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?

Ben Lowry
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The Dictionary object is simple to use, but features no sorting methods.

Check out this page for directions on how to setup the array to store the sorting stuff.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
benlowryAuthor Commented:
Thanks!  Appreciate the quick response too!
benlowryAuthor Commented:
I have a sub I use to sort a dictionary object:

Call it using

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

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

End Sub
Sub ReBuildArray(objDict, aTempArray)

  Set SortCart = Server.CreateObject("Scripting.Dictionary")

  for each Prod in objDict
    SortCart.add Prod, objDict(Prod)

  Dim iTemp
  For iTemp = 0 To UBound(aTempArray)
    objDict.add aTempArray(iTemp), SortCart.Item(aTempArray(iTemp))

  set SortCart = nothing

End Sub
Sub SortOrder(objDict)
  Dim aTemp
  Call BuildArray(objDict, aTemp)
  Call SortArray(aTemp)
  Call ReBuildArray(objDict, aTemp)
End Sub
benlowryAuthor Commented:
ahhahaha this was my question

i'm so tired :(
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.