码迷,mamicode.com
首页 > 编程语言 > 详细

List.Sort 排序用法收集

时间:2015-05-23 22:31:33      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

使用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

List.Sort 排序用法收集

标签:

原文地址:http://www.cnblogs.com/fery/p/4524923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!