zoukankan      html  css  js  c++  java
  • C#利用iComparable接口实现List排序

    List<T>类可以使用Sort()方法对元素排序。

    Sort()方法定义了几个重载方法,分别是
      public void List<T>.Sort(),不带有任何参数的Sort方法
      public void List<T>.Sort(Comparison<T>),带有比较代理方法参数的Sort方法  
      public void List<T>.Sort(IComparer<T>), 带有比较器参数的Sort方法 
      public void List<T>.Sort(Int32,Int32,IComparer<T>),带有比较起参数,可以指定排序范围的Sort方法 

    第一种方法必须继承IComparable<>接口,下面是示例代码:

    // <copyright file="ServiceOfferingContent.cs" company="Deloitte">
    //     Copyright (c) Deloitte. All rights reserved.
    // </copyright>
    // <author>Haibin Chen</author>
    // <email>Claudchen@Deloitte.com.cn</email>
    // <date>2016-08-08</date>
    // <summary>Entities used for connect WebParts and Lists content.</summary>
    
    using System;
    using System.Collections.Generic;
    using Deloitte.Gdc.KM.Utils;
    
    namespace Deloitte.Gdc.KM.WebParts.Entities
    {
        /// <summary>
        /// Class ServiceOfferingContent.
        /// </summary>
        public class ServiceOfferingContent :  IComparable<ServiceOfferingContent>
        {
    
            /// <summary>
            /// Gets or sets the index.
            /// </summary>
            /// <value>The index.</value>
            public string Index { get; set; }
    
            /// <summary>
            /// Compares to.
            /// </summary>
            /// <param name="other">The other.</param>
            /// <returns>System.Int32.</returns>
            public int CompareTo(ServiceOfferingContent other)
            {
                if (other == null) return -1;
                if (Index.ToInt() > other.Index.ToInt())
                {
                    return -1;
                }
                return 1;
            }
        }
    }
  • 相关阅读:
    AIBigKaldi(二)| Kaldi的I/O机制(源码解析)
    OfficialKaldi(十四)| 从命令行角度来看Kaldi的 I / O
    GNU Make函数、变量、指令
    C/C++编码规范(google)
    [English]precede, be preceded by
    视频压缩技术、I帧、P帧、B帧
    SMB
    printf占位符
    使用 Yocto Project 构建自定义嵌入式 Linux 发行版
    gcc fpic fPIC
  • 原文地址:https://www.cnblogs.com/bincoding/p/5826063.html
Copyright © 2011-2022 走看看