I have some custom business objects and need to implement some sorting logic. The sort requirement that I have requires the ability to sort on multiple columns as well. I am sure I am not the first that needs to do this, but so far I have not been able to find anything on the web that doesn't rely on a DB to do the sorting. That is not an option for me, so I need to implement this my self.
Thanks for any help
Kevin

Business Object Sorting
curt1
Thanks for the link to the code project site as that looks like a good implementation. I have sorting implemented now in the business object but when binding it to a datagridview the sorting is really really slow and I am not sure how to go about speeding it up. I have a list of 170 items and when using the built in sorting provided by the grid it takes 5 seconds (no official timings, just counting myself). When I went around the grids built in sorting and called the sort on the business object myself from the click event of the column it got down to about 3 seconds. This all seems too slow for only 170 items.
Any Ideas
HolySmokes
In .net 2005 it is possible and easy to sort them....
By using Generic List<T>
it makes what you want
MyList.Sort(
delegate(MyBusinessObject o1,MyBusinessObject o2){
int r1 = o1.property1.CompareTo(o2.property1); if (r1 == 0){
int r2 = o1.property2.CompareTo(o2.property2); if (r2 == 0){
int r3 = o1.property3.CompareTo(o2.property3); return r3;}
else{
return r2;}
}
else{
return r1;}
}
);
I agree that it seems complex but it may give you an idea about it...
Here is a project that may help you more:
you just type your code as like this:
DynamicComparer<MyBusinessObject> x=new DynamicComparer<MyBusinessObject>("property1 ASC,property2 ASC, property3 ASC");
MyList.Sort(x);
http://www.codeproject.com/useritems/DynamicListSorting.asp
DimitrisTsangaris
If this is the case, you can create a class that implements IComparer interface. You can then use this class as an argument in the Sort() method of the ArrayList.
skmichi
chessmaster
You may sort your list by the project of which i gave the link, and then bind it to the gridview... The project is really really fast, and easy-to-use, so you'd better use it... Builtin sort algorithm may be slow due to some issues(i dont know what they exactly are).
Ive tested the project and my code and they both are fast enough(0.05 seconds or something smaller than 0.1 second).