• List.Sort 排序用法收集


    使用Lambda表达式,实现代码如下:

    private static void SortByLambda()
            {
                List<Article> list = GetArticleList();
                list.Sort((x, y) =>
                {
                    int value = x.SortIndex.CompareTo(y.SortIndex); 
                    if (value == 0)
                        value = x.Comments.CompareTo(y.Comments);
                    return value;
                });
            }

    ---第二种方法

    public class Article : IComparable<Article>
        {
            public string Title { get; set; }
            public int Comments { get; set; }
            public int SortIndex { get; set; }

            public override string ToString()
            {
                return string.Format("文章:{0},评论次数:{1}", this.Title, this.Comments);
            }
            
            public int CompareTo(Article other)
            {
                if (other == null)
                    return 1;
                int value = this.SortIndex - other.SortIndex;
                if (value == 0)
                    value = this.Comments - other.Comments;
                return value;
            }
        }

    参考网址:http://www.cnblogs.com/supperwu/archive/2012/06/13/2548122.html

  • 相关阅读:
    BZOJ 5418: [Noi2018]屠龙勇士 EXCRT+multiset
    CF1033D Divisors Pollard-rho
    BZOJ 3782: 上学路 Lucas+ExCRT+容斥+dp
    BZOJ 1951: [Sdoi2010]古代猪文 ExCRT+欧拉定理+Lucas
    Activiti介绍(一)
    Centos7卸载FastDFS6.1卸载(六)
    FastDFS整合SpringBoot(五)
    FastDFS整合nginx模块报错
    SpringBoot怎么访问html文件
    FastDFS整合普通Maven项目(四)
  • 原文地址:https://www.cnblogs.com/fery/p/4524923.html
Copyright © 2020-2023  润新知