Alex,
I have included a sample below of sorting using TList. It's not as clean as I think that it could be, but it' just using the base interfaces. I don't know why we couldn't provide an overload of ApplySort that would allow the Order By syntax. It's just a matter of implementing it. I would like to spend some time looking at the article you mentioned to look at their implementation of the Comparer class.
TList<CIRStatus> list = DataRepository.CIRStatusProvider.GetAll();
PropertyDescriptorCollection propColl = TypeDescriptor.GetProperties(typeof(CIRStatus)); //Simple Sort list.ApplySort(propColl["CIRStatusID"], ListSortDirection.Descending); //Complex Sort ListSortDescription[] sortDescs = new ListSortDescription[2]; sortDescs[0] = new ListSortDescription(propColl["CIRStatusDescription"],ListSortDirection.Ascending); sortDescs[1] = new ListSortDescription(propColl["CIRStatusID"], ListSortDirection.Ascending); list.ApplySort(new ListSortDescriptionCollection(sortDescs));
//Simple Sort
list.ApplySort(propColl["CIRStatusID"], ListSortDirection.Descending); //Complex Sort ListSortDescription[] sortDescs = new ListSortDescription[2]; sortDescs[0] = new ListSortDescription(propColl["CIRStatusDescription"],ListSortDirection.Ascending); sortDescs[1] = new ListSortDescription(propColl["CIRStatusID"], ListSortDirection.Ascending); list.ApplySort(new ListSortDescriptionCollection(sortDescs));
//Complex Sort ListSortDescription[] sortDescs = new ListSortDescription[2]; sortDescs[0] = new ListSortDescription(propColl["CIRStatusDescription"],ListSortDirection.Ascending); sortDescs[1] = new ListSortDescription(propColl["CIRStatusID"], ListSortDirection.Ascending); list.ApplySort(new ListSortDescriptionCollection(sortDescs));
ListSortDescription[] sortDescs = new ListSortDescription[2]; sortDescs[0] = new ListSortDescription(propColl["CIRStatusDescription"],ListSortDirection.Ascending); sortDescs[1] = new ListSortDescription(propColl["CIRStatusID"], ListSortDirection.Ascending); list.ApplySort(new ListSortDescriptionCollection(sortDescs));
sortDescs[0] = new ListSortDescription(propColl["CIRStatusDescription"],ListSortDirection.Ascending); sortDescs[1] = new ListSortDescription(propColl["CIRStatusID"], ListSortDirection.Ascending); list.ApplySort(new ListSortDescriptionCollection(sortDescs));
sortDescs[1] = new ListSortDescription(propColl["CIRStatusID"], ListSortDirection.Ascending); list.ApplySort(new ListSortDescriptionCollection(sortDescs));
list.ApplySort(new ListSortDescriptionCollection(sortDescs));
Ben Johnson------------------------------ Member of the .NetTiers team Visit http://www.nettiers.com------------------------------
So, you would like SortComparer to implement the IComparer interface? Is this in addition to the IComparer<T>, which it already implements? If so, have you already done this? Also, why is this interface needed? Any shared code would be appreciated.
Yep, I misunderstood what you were asking. It makes much more sense now. I missed the "public" part... Oh well....
I decided to stay with the SortComparer class, just because I understand how it works. When I started looking at the DynamicSorter and saw that it emits IL code directly, I got a little itimidated (especially from a support standpoint if something doesn't work). However, I went ahead and added the ability to pass in a SQL-like order by clause to TList and SortComparer. Hopefully, it will give you the functionality you need.
SortComparer:
public SortComparer(string orderBy)
TList:
public void Sort(string orderBy)
public void Sort(Comparison<T> comparison)
public void Sort(IComparer<T> comparer)
So, now you can do things like:
TList<MyObject> list = DataRepository.MyObjectProvider.GetAll();
list.Sort("Property1 desc, Property2 asc, Property3 desc");
//OR
SortComparer sortComparer = new SortComparer("Property2 desc");
list.Sort(sortComparer);
Ben,
For nullable columns, CompareValues in TList.cst throws exception. Need to check null before calling value.ToString(). With that modification, all work well.
Roy
Roy,
Thanks, it should be fixed now.
Hi, Ben,
I don't know how to use TList or VList Sort method working with a GridView (see my post http://community.codesmithtools.com/forums/thread/10139.aspx). Could you provide more detailed information? Forgive me if this is naive.
Thanks,Kangming