advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the code for this article
Sorting in .NET may be simple, but it still leaves much to be desired. Not being able to sort multidimensional arrays and hashtables is an inconvenience. What challenges have you encountered involving sorting in .NET? Let us know in the .NET Discussion Forum!
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 4.6/5 | Rate this item | 7 users have rated this item.
 

Sorting in the .NET Framework

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.  


advertisement
n .NET, sorting arrays or collections of primitive types is a relatively simple task. Most developers understand that for a collection of numbers or strings, the runtime will know how to compare these values. However, some developers are left wondering how to sort complex objects. For example, what if you wanted to sort an array of FileInfo objects by size rather than name?


After witnessing one fellow developer attempt to code his own BubbleSort algorithm because he could not find another way to sort an array of his custom objects, I felt this article would help spread the word about the rich sorting capabilities supported by .NET.

Sorting Arrays and ArrayLists
Sorting an array composed of primitive data types such as strings and integers is straightforward. The Array class contains a static method called Sort. This method takes in an existing array object and sorts it. The following code demonstrates how simple it can be:


Dim aryBeatles() As String = {"John", "Paul", "George", "Ringo"}
Array.Sort(aryBeatles)

' New order of the aryBeatles array:
' 
' aryBeatles
' ----------
' George
' John
' Paul
' Ringo
Sorting an ArrayList is equally easy but somewhat different. The ArrayList is based on an Array. It internally manages an array while exposing properties and methods to make working with them easier. Taking from the example above, here is a demonstration of sorting an ArrayList:

'Base the arraylist on the aryBeatles array.
Dim lstBeatles As New ArrayList(aryBeatles)
lstBeatles.Sort()
Aside from the syntactical differences, the ArrayList still internally relies on the Array.Sort method to sort its contents. The Array.Sort method is the core of all sorting in. NET. Because of this, for the rest of the article I will use arrays to demonstrate the sorting capabilities of the .NET Framework.

  Next Page: Key-value Pair Sorting


Page 1: IntroductionPage 3: Customized Sorting with IComparer
Page 2: Key-value Pair SortingPage 4: Making Objects Comparable Using IComparable
Please rate this item (5=best)
 1  2  3  4  5
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs