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 -;
if (Index.ToInt() > other.Index.ToInt())
{
return -;
}
return ;
}
}
}

C#利用iComparable接口实现List排序的更多相关文章

  1. 【转载】C#中自定义Sort的排序规则IComparable接口

    C#中的List集合在排序的时候,如果不使用Lambda表达式进行排序的话,一般调用Sort()方法进行排序,如果希望Sort()方法排序后的结果跟我们预想的效果一致或者按照我们自定义的规则排序,则需 ...

  2. IComparable接口实现自定义类型的排序

    IComparable接口实现自定义类型的排序   CompareTo(Object) 方法的实现必须返回有三个值之一 如下表中所示. 返回值 参数比较 大于0 x>y 等于0 x=y 小于0 ...

  3. 我的Java开发学习之旅------>Java利用Comparator接口对多个排序条件进行处理

    一需求 二实现Comparator接口 三验证排序结果 验证第一条件首先按级别排序级别最高的排在前面 验证第二条如果级别相等那么按工资排序工资高的排在前面 验证第三条如果工资相当则按入职年数排序入职时 ...

  4. C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法

    在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...

  5. 这个匿名对象没有实现IComparable接口

    https://www.cnblogs.com/felixnet/p/5193086.html https://docs.microsoft.com/zh-cn/dotnet/api/system.i ...

  6. 通过实现System.IComparable接口的CompareTo方法对两个类进行比较

    假设现在有一个学生类 class Student { int age; public Student(int age) { this.age = age; } } 要使学生类之间能进行比较,实现Sys ...

  7. SortedMap接口:进行排序操作。

    回顾:SortedSet是TreeSet的实现接口,此接口可以排序. SortedMap接口同样可以排序,是TreeMap的实现接口,父类. 定义如下: public class TreeMap< ...

  8. 利用脚本修改SQL SERVER排序规则

    利用脚本修改SQL SERVER排序规则 编写人:CC阿爸 2014-3-1 l  今年的一项重要工作是对公司所用系统进行繁简的转换,程序转成简体基本很容易解决,但数据库转换成简体,就没那么容易了.经 ...

  9. 新的IRepository接口+搜索和排序解耦(+基于Castle实现)

    新的IRepository接口+搜索和排序解耦(+基于Castle实现) 经过了上篇IRepository和IRepository<T>的讨论[文章地址为:http://www.cnblo ...

随机推荐

  1. 单源最短路径算法——Dijkstra算法

    #include <stdio.h> #include <stdlib.h> #include <string.h> /* run this program usi ...

  2. 初探linux内核编程,参数传递以及模块间函数调用

    一.前言                                  我们一起从3个小例子来体验一下linux内核编程.如下: 1.内核编程之hello world 2.模块参数传递 3.模块间 ...

  3. [转][前端优化]使用Combres合并对js、css文件的请求

    本文转自:http://www.cnblogs.com/parry/archive/2011/01/28/Reduce_Http_Request_Using_Combres_For_Js_Css.ht ...

  4. AC日记——向量点积计算 openjudge 1.6 09

    09:向量点积计算 总时间限制:  1000ms 内存限制:  65536kB 描述 在线性代数.计算几何中,向量点积是一种十分重要的运算. 给定两个n维向量a=(a1,a2,...,an)和b=(b ...

  5. 为什么使用Sass

    为什么使用Sass 作为前端(html.javascript.css)的三大马车之一的css,一直以静态语言存在,HTML5火遍大江南北了.javascript由于NODE.JS而成为目前前后端统一开 ...

  6. Unity StartCoroutine 和 yield return 深入研究

    http://www.cnblogs.com/fly-100/p/3910515.html

  7. oracle的substr函数的用法

    oracle的substr函数的用法 取得字符串中指定起始位置和长度的字符串   substr( string, start_position, [ length ] ) 如:     substr( ...

  8. Centos5.8 安装 PHP5.5 和 memcached

    安装GIT 需要先安装gcc-c++ (sudo yum install gcc-c++) sudo yum install gettext-devel expat-devel cpio perl o ...

  9. codevs 1215 迷宫

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,-,xn,以及一个整数 k(k<n).从 n ...

  10. 2016.11.14测试 长乐一中2014NOIP复赛模拟题 第一题。

    1.正确答案 [题目描述] 小H与小Y刚刚参加完UOIP外卡组的初赛,就迫不及待的跑出考场对答案. "吔,我的答案和你都不一样!",小Y说道,"我们去找神犇们问答案吧&q ...