按ASC或DESC排序列表

时间:2010-03-06 05:57:01

标签: c#

是否可以按ASC或DESC对List进行排序?

private class CompAdvertisements : IComparer<Advertisements>
        {
            #region IComparer<Advertisements> Members

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

            #endregion
        }

2 个答案:

答案 0 :(得分:2)

两个非常简单的选择:实现ReverseAdvertisementsComparer类:

private class ReverseAdvertisementsComparer: IComparer<Advertisements>
{
    public int Compare(Advertisements x, Advertisements y)
    {
        // notice: reversed x and y
        return y.Country.Name.CompareTo(x.Country.Name);
    }
}

或者只需拨打List<Advertisements>.Sort(),然后拨打List<Advertisements>.Reverse()

答案 1 :(得分:0)

return y.Country.Name.CompareTo(x.Country.Name);

要扭转它