When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Wednesday, June 27, 2001

Sorting a Dictionary Object, Part 2

By Aaron A.

  • Read Part 1

  • In Part 1 we looked at how to build up the temporary array that holds the keys of the Dictionary object; we then examined how to sort this array using a bubble sort. In this part, we'll look at how to display the dictionary object in a sorted fashion. Additionally, a helper function is presented as well as a live demo!

    - continued -

    Displaying the Dictionary Contents
    Finally, we display the contents of the dictionary using the array that we just created as an index for the dictionary. The code for PrintDictionary is below; note that the procedure expects two parameters: the dictionary object to display and the array that holds the sorted indexes of the dictionary object being passed in.

    Sub PrintDictionary(objDict, aTempArray) 
      Dim iTemp 
      For iTemp = 0 To UBound(aTempArray) 
        Response.Write(aTempArray(iTemp) & " - " & _
                       objDict.Item(aTempArray(iTemp)) & "<br>") 
    End Sub

    To print the results, we just loop through the array and print the corresponding item in the dictionary object. To print the corresponding dictionary item, we use the code objDict.Item(aTempArray(iTemp)). After running this you should see a list of alphabetized keys and their associated items. To actually insert data and call these three procedures, you could use the following code:

    'Create our dictionary and array variables
    Dim dObj, aTemp
    Set dObj = Server.CreateObject("Scripting.Dictionary") 
    '-- Get some values
    dObj.Add "Apple", "Value1" 
    dObj.Add "Orange", "Value2" 
    dObj.Add "Banana", "Value3"
    dObj.Add "Grapefruit", "Value4" 
    dObj.Add "Avacado", "Value5"
    Call BuildArray(dObj, aTemp) 	' Build the array
    Call SortArray(aTemp) 	' Sort the array 
    Call PrintDictionary(dObj) ' Print the dictionary using the array as an index 
    Set dObj = Nothing	' Dereference the object
    [View a live demo]

    I also took the initiative to create a procedure that would, essentially, wrap these three procedure calls into one. This procedure, PrintSortedDictionary, expects a lone parameter: the dictionary object you wish to sort and then display. The code for PrintSortedDictionary can be seen below:

    Sub PrintSortedDictionary(objDict)
      Dim aTemp
      Call BuildArray(objDict, aTemp)
      Call SortArray(aTemp)
      Call PrintDictionary(objDict, aTemp)
    End Sub

    And that should do it. Create the dictionary object, add some values to it, and then run the three procedures on it (or the single PrintSortedDictionary procedure). The result will be an ordered list of keys and their corresponding values. This will also work on numerals since strComp will correctly compare those as well.

    If you have further questions, feel free to drop by #ASP on Efnet and look for Frijoles, although there are many of us there that try to help out.

    Good luck and happy programming!

  • By Aaron A.


  • View the live demo!
  • Read Sorting a One-Dimensional Array Using Bubble Sort

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article