zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    从《兄弟连》到团队管理
    将来
    [译] TypeScript入门指南(JavaScript的超集)
    基于cocos2dx迷宫游戏
    SVN版本管理教程
    arcgis for android 本地缓存
    vs2010变的特别卡解决办法
    cocos2d-x自适应屏幕
    cocos2d-x使用CCScale9Sprite
    cocos2dx开发入门文档
  • 原文地址:https://www.cnblogs.com/fery/p/4524923.html
Copyright © 2011-2022 走看看