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

  • 相关阅读:
    山寨 《寻找房祖名》
    css3 弹性效果上下翻转demo
    CSS3 Hover 动画特效
    判断一个字符串通过变化字符的位置,是否可以组成回文
    获取多个字符串中的共同字符
    转换为回文的步数
    IOS中图片的一些处理方法
    python django的一点笔记
    一个图片切割的例子
    一个批量修改文件夹中文件名的命令
  • 原文地址:https://www.cnblogs.com/fery/p/4524923.html
Copyright © 2011-2022 走看看