Sort Collection

I had a need to be able to sort a collection by two different properties within the class. Basically having the collection sorted by one property and then by the 2nd property. I never ended up implementing this in a production application but in all of my testing it did work. You download the code SortCollection.txt (4 KB)

To use it, you add a SortClass to the Comparer. You can add as many as you want. A SortClass is made up of the property name and a direction. So, for example:

public MyCollectionClass SortMyClass(MyCollectionClass myClass)
{
Comparer comparer = new Comparer();
comparer.SortClasses.Add(new SortClass("MyProperty1", SortDirection.Ascending); 
comparer.SortClasses.Add(new SortClass("MyProperty2", SortDirection.Descending); 
myClass.Sort(comparer);      
return myClass;     
}
So after the sort, myClass would be sorted by MyProperty1 (ascending), then by MyProperty2 (descending)

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

November 21. 2008 08:20 PM