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

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Wednesday, June 27, 2001

Sorting a Dictionary Object

By 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?

- continued -

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 (PrintSortedDictionary).

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:

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

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 property. 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 SortArray 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 BuildArray.

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

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. The second 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 doing.

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.

  • Part 2!


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