Sorting a Dictionary ObjectBy Aaron A.
Recently an individual came on to the IRC channel which I frequent and asked a question which I had never considered: How do you sort a dictionary object by its keys? At first I didn't think much of this because I've never really thought about sorting a dictionary object, but then the idea started to build. How exactly did one go about sorting a dictionary object?
After a small amount of thought I decided that an easy approach to this problem would be to use an array as
an index for the dictionary object. Instead of reordering your dictionary object (if such a thing is
possible), simply reorder your array. I achieved this with three separate procedures: one to build the array
BuildArray), one to sort the array (
SortArray), and one to print the dictionary contents
using the array as the index (
PrintDictionary). I also wrote a single procedure that simply called
all three of these procedures so that you could, with one line of code, sort the dictionary object
Building the Array
The first step in this tutorial is to build the array, which is passed into each of the three procedures. For this example, then, you will need to create an array. Since we will be dynamically resizing the array in the
BuildArray procedure, do not give your array any bounds, simply declare it like:
Dim ArrayName. The code for the
BuildArray procedure can be seen below. It
expects two parameters - the dictionary object we wish to sort and the temporary, index array:
First we create a counter variable,
nCount, that will be used to increment the array. Next we
ReDim our array to the size of the dictionary object using the dictionary object's
Count, as its name suggests, will return how many keys we have in the dictionary object.
After that, we loop through the dictionary object using a
For Each ... Next loop and place each
key into the array.
Sorting the Array
After we have the array filled with our data, the next step is to sort it. In this case I am using a bubble sort. There are numerous articles available both here on 4Guys and on other sites regarding sorting methods. Be sure to check out Sorting a One-Dimensional Array Using Bubble Sort and Sorting Arrays with QuickSort. A bubble sort is one of the easiest sorting algorithms, hence the reason I chose to use it. Below you will find the code for the
procedure, which takes a single parameter: the array that we wish to sort. Note that this array should be the
same array that we passed into
This bit of code looks a little confusing at first. If you have never done a bubble sort, it may be best to work
through it by hand on a piece of paper first. The first step is to declare some variables which we will use.
Sorting requires two
For loops to work correctly. Our first loop will move all the way through the array from
beginning to end.
For loop will only go as high as the first loop, but it will be done every time
through the first loop.
Next comes the real brains of the procedure. We compare two places in the array (which are side by side) together.
If the first item in the array is larger than the second, we swap them using a temporary string variable. This is
where the sorting is actually taking place.
This comparison is done between all pairs of items in the array until
they have all been checked and sorted. To picture this, imagine moving your two index fingers across the contents
of an array. When you increment to the next number, check your fingers. If the value being pointed at by the
finger on the left (left being closer to
0 in the array) is smaller than the value on your right
finger, swap them. Continue to move your fingers along the array until you reach the end, then move back to the
spot one to the right of your left finger and start over. This is what the two
For loops are
Now that we have our array built up and sorted, all that's left is to display the dictionary object's elements based on the ordering of this array. In Part 2 we'll look at how to accomplish this.