开发者

sorty List by ASC or DESC

开发者 https://www.devze.com 2022-12-22 15:01 出处:网络
Is it possible to sorty List by ASC or DESC? private class CompAdvertisements : IComparer<Advertisements>

Is it possible to sorty List by ASC or DESC?

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
        }


Two pretty straightforward choices: implement a ReverseAdvertisementsComparer class:

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);
    }
}

Or just call List<Advertisements>.Sort() followed by List<Advertisements>.Reverse().


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

To reverse it

0

精彩评论

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