Browse DevX
Sign up for e-mail newsletters from DevX


Sorting in the .NET Framework  : Page 2

You don't need to code your own sorting routine—the .NET Framework can do it for you! Read this article to get a basic, yet comprehensive, understanding of sorting and comparing objects in the .NET Framework.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Key-value Pair Sorting
One of the features of sorting in the .NET framework is the ability to sort one array based on the values of another array of equal length. This is called key-value sorting. Basically, you have an array of keys and an array of values. The key value in the key array at a given index is used to look up the value in the value array at the same index. Consider the example in Listing 1.

As you can see, it sorts the key array and changes the index in the value array so that their mapping stays consistent. This type of sorting is useful if you implement your own hashtable or dictionary collection, and the underlying collections are a pair of arrays corresponding to the keys and values.

Figure 1. Partial Sorting: This illustration gives a visual picture of how a portion of an array's content can be sorted.

Sorting a Portion of a Collection
Sometimes you may want to sort only a portion of a collection. Figure 1 illustrates partial sorting. Typically, you could accomplish this by copying the range of values you want sorted into a temporary array, sort it, and copy them back. However, in .NET you can accomplish this in one line of code:

Dim aryNumbers() As Integer = {13, 4, 7, 10, 2, 8, 17, 5, 1, 11} ' Using the Array.Sort(array, index, length) method call to sort ' only elements 2-6. Array.Sort(aryStateKeys, 2, 5) ' New order of the aryNumbers after partial sorting: ' ' aryNumbers ' ---------- ' 0: 13 ' 1: 4 ' 2: 2 ' 3: 7 ' 4: 8 ' 5: 10 ' 6: 17 ' 7: 5 ' 8: 1 ' 9: 11

Automatic Sorting Using a SortedList
The .NET Framework contains a collection called a SortedList. Instead of calling sort when needed to sort the collection, the collection stores all its values already presorted. As you add new values to the collection, it internally finds the appropriate spot to insert the object. This class is useful when you need the objects contained within the collection to be pre-sorted at all times.

The SortedList acts more like a Hashtable rather than an ArrayList. The reason for this is that the collection stores key-value pairs rather than the values themselves. Because the Hashtable and DictionaryBase classes do not have sorting built-in, it may be more suitable to use SortedList when you require the keys to be sorted.

This code shows how to use a SortedList:

Dim slsStates As New SortedList slsStates.Add("CA", "California") slsStates.Add("AZ", "Arizona") slsStates.Add("NV", "Nevada") ' slsStates order: ' ' Keys | Values ' --------------- ' AZ | Arizona ' CA | California ' NV | Nevada

Curiously, Microsoft left out sorting in Hashtables and other dictionary objects. At the time of writing, I was unable to confirm whether or not that sorting capability will be added in .NET 2.0. Neglecting to add this capability prolongs developer inconvenience and will force us to create an ad hoc workaround.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date