You have to create your own Sort method.
I typically do the following:
1) make the collection class implement IComparer which means I have to
create a method of this signature:
public int Compare(object x, object y) { ...}
2) add an child enum of the class called SortOptions adding each
property I want to be able to sort on
3) add methods to add or clear items from the sort priority.
4) implement sort to call "this.InnerList.Sort(this);" This passes the
current class into the base class sort method, so the results of the
Compare method in step one determine the order of items in the
collection.
This example shows how to make your collection sortable on multiple
property values at the same time. It could be more simple if you only
needed to sort on one property at a time. Just replace the sort option
list, with a single sort option property.
public class PersonCollection:CollectionBase, IComparer
{
private ArrayList sortOptions_ = new ArrayList();
public enum SortOptions
{
FullNameAsc, FullNameDesc, JobTitleAsc, JobTitleDesc
}
public void AddSort(SortOptions option)
{
this.sortOptions_.Add(option);
}
public void ClearSort()
{
this.sortOptions_.Clear();
}
public void Sort()
{
if (this.sortOptions_.Count == 0)
return;
this.InnerList.Sort(this);
}
public int Compare(object x, object y)
{
int retVal = 0;
Person px = (Person)x;
Person py = (Person)y;
for (int i=0; i < this.sortOptions_.Count; i++)
{
SortOptions option = (SortOptions)this.sortOptions_;
switch(option)
{
case SortOptions.FullNameAsc:
retVal = px.FullName.CompareTo(py.FullName);
break;
case SortOptions.FullNameDesc:
retVal = py.FullName.CompareTo(px.FullName);
break;
case SortOptions.JobTitleAsc:
retVal =
px.Title.FamilyDescription.CompareTo(py.Title.FamilyDescription);
break;
case SortOptions.JobTitleDesc:
retVal =
py.Title.FamilyDescription.CompareTo(px.Title.FamilyDescription);
break;
}
//if not equal after the current sortoption, then done
// comparing. (Stop at the first sortoption where differ)
if (retVal != 0)
break; // from sort option loop
}
return retVal;
}
}