in

Paul Rausch

Sorting Collections

With the new language features in the .Net framework this has become much simpler.  I had to implement sorting on a custom collection in two projects last week, one being .net 1.1 and one being .net 3.5.  Let me tell you it was much easier in 3.5 and much more flexible as you can order by multiple fields without doing multiple IComparers.

In .net 1.1 I had to implement a custom IComparer class to do the comparison, which involves creating a whole new class and adding logic to do the sorting.

In .net 3.5 all I had to do was this:

myCollection = myCollection.OrderBy(obj => obj.FieldToSort).ToList()

or

myCollection  = (from obj in myCollection orderby obj.FieldOne, obj.FieldTwo select obj).ToList()

Only published comments... Apr 02 2008, 10:17 AM by prausch
Filed under: ,

Leave a Comment

(required)  
(optional)
(required)  
Add
Inetium, LLC. Disclaimer