Sorting a Dictionary Object, Part 2By Aaron A.
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!
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
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
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:
I also took the initiative to create a procedure that would, essentially, wrap these three procedure calls into one.
PrintSortedDictionary, expects a lone parameter: the dictionary object you wish to
sort and then display. The code for
PrintSortedDictionary can be seen below:
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!