开发者

c# list comparer use two compare elements

private class CompAdvertisements : IComparer<Advertisements>
{
private string OrderBy { get; set; }

public CompAdvertisements(string orderBy)
{
OrderBy = orderBy;
}

#region IComparer<Advertisements> Members

public int Compare(Advertisements x, Advertisements y)
{
return x.Country.Name.CompareTo(y.Country.Name);

Can i also user x.Name.CompareTo(y.Name); in comparer that i will compare with two elements lik order by something and 开发者_JS百科order by something2


Yes. If the outer comparison indicates that your two elements (Country.Name) are the same, then you instead return the result of an inner comparison (somethingElse). You can do that for an arbitrary depth of comparisons.

outerCompare = x.Country.Name.CompareTo(y.Country.Name);
if (outerCompare != 0)
{
    return outerCompare;
}
else
{
    return (x.Name.CompareTo(y.Name));
}


Eric J. is right. You may also want to have a look at this stackoverflow question. The answers there give several ways you can sort a List, and they also go into detail about using an IComparer object to perform a similar task to what you're doing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜