Custom Sorting (IComparer on three fields)
Posted
by
Kave
on Stack Overflow
See other posts from Stack Overflow
or by Kave
Published on 2010-12-21T16:29:33Z
Indexed on
2010/12/22
8:54 UTC
Read the original article
Hit count: 217
I have a person class with three fields, Title, Name, Gender and I would like to create a Custom Sort for it to sort it first by Title, then by Name and then by Gender ascending:
public class SortPerson : IComparer
{
public int Compare(object x, object y)
{
(…)
}
}
I know how to do this for only one variable to compare against: But How would I have to proceed with three?
public class SortPerson : IComparer
{
int IComparer.Compare(object a, object b)
{
Person p1=(Person)a;
Person p2=(Person)b;
if (p1.Title > p2.Title)
return 1;
if (p1.Title < p2.Title)
return -1;
else
return 0;
}
}
Many Thanks,
© Stack Overflow or respective owner