Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

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
0
benlowry
Asked:
benlowry
  • 3
1 Solution
 
rbagdonasCommented:
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.

http://www.4guysfromrolla.com/webtech/062701-1.shtml
0
 
benlowryAuthor Commented:
Thanks!  Appreciate the quick response too!
0
 
benlowryAuthor Commented:
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
'=======================================================
%>
0
 
benlowryAuthor Commented:
ahhahaha this was my question

i'm so tired :(
0
 
ViRoyCommented:
ROFL
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now