one trick is to string together the string properties, and sort on them.
I'm writing this mostly from memory, so hopefully its nearly all correct.
but you should see the idea.
( case LastNameAndFirstName is what I'm talking about).
At the least, you'll see how to write 1 comparer for multiple properites ..
using the Enum thing.
...
using System;
using System.Collections;
namespace MyApplication.Comparers
{
internal sealed class EmployeeComparer : IComparer
{
private EmployeeSortColumns m_sortValue = EmployeeSortColumns.None ;
public enum EmployeeSortColumns
{
None = 0 , LastName = 1 , FirstName = 2 , LastNameAndFirstName = 3
}
public EmployeeComparer(EmployeeSortColumns sortValue)
{
m_sortValue = sortValue;
}
public int Compare(object x,object y)
{
switch(m_sortValue)
{
case EmployeeSortColumns.None :
return 0;
case EmployeeSortColumns.FirstName :
return ((Employee)x).FirstName .CompareTo(((Employee )y).FirstName );
//break;
case EmployeeSortColumns.LastName :
return ((Employee)x).LastName.CompareTo(((Employee )y).LastName );
case LastNameAndFirstName :
string xValue = ((Employee)x).LastName + ((Employee)x).FirstName;
string yValue = ((Employee)y).LastName + ((Employee)y).FirstName;
return xValue.CompareTo(yValue);
default:
return ((Employee)x).LastName .CompareTo(((Employee )y).LastName );
// break;
}
}
}
}
...
Other comments:
While I'm usually in favor of using strong objects and collections, this is
one area where the strongly typed dataset is helpful, since
it has a .Select ( filter, sortby ) method.
MyStrongDS.EmpTable.Select ("" , "LastName , FirstName DESC , City");
something like that.
"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi
say I have a list of Person objects, which I want to sort. But I don't
just want to sort by "name" say, but by several fields in the Person object.
Maybe 1st priority by height, 2nd priority by weight, and 3rd priority by
age.
A Person object has many attributes though, and I want to be able to
select between all of the fields for sorting (with 1st, 2nd, 3rd priority).
What would be a good approach?
Thanks,
Peter