C# List.Sort与IComparer接口及Comparison委托应用于集合排序
C#里List.Sort的用法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ListSort
{
class Program
{
static void Main(string[] args)
{
List<C> L = new List<C>();
L.Add(new C { n = 1, s = "b" });
L.Add(new C { n = 3, s = "a" });
L.Add(new C { n = 2, s = "c" });<br>
// 方法1 使用Comparison<T>委托。
L.Sort((left, right) =>
{
if (left.n > right.n)
return 1;
else if (left.n == right.n)
return 0;
else
return -1;
}); // 方法2 使用IComparer<T>接口。
L.Sort(new CComparer()); // 方法3 除以上两种方法以外还可以使用另一种方法,在C类中实现IComparable<T>
L.Sort();
}
}
//方法二
public class CComparer : IComparer<C>
{
public int Compare(C left, C right)
{
if (left.n > right.n)
return 1;
else if (left.n == right.n)
return 0;
else
return -1;
}
}
//方法三
public class C : IComparable<C>
{
public int n;
public string s; public int CompareTo(C other)
{
if (this.n > other.n)
return 1;
else if (this.n == other.n)
return 0;
else
return -1;
}
}
}
IComparer接口及Comparison委托应用于集合排序
需要对list进行排序,可以用list.Sort()方法。该方法有多个重载。
(1)使用IComparer<T>接口
可以为Sort传入IComparer<T>的实现类的实例对象,该接口为:
public interface IComparer<in T>
{ //如果x小于y,则返回负数;x大于y,返回正数;等于则返回0
int Compare(T x, T y); }
void Sort(IComparer<T> comparison);
如果使用IComparer<T>实现类的实例对象,则需要实现定义好实现类,对于已经定义好的,则比较方便,否则相对麻烦一些。
(2)使用Comparison<T>委托
但是对于没有定义IComparer<T>的实现类的场合,可以使用更为方便的方式,即使用Comparison<T>委托作为参数。
public delegate int Comparison<in T>(T x, T y);
void Sort(Comparison<T> comparison);
具体使用时,可以直接传入委托(或函数名称),也可以直接使用numda表达式。以下是使用Lambda表达式的方法代码:
List<Student> list=new List<Student>(); //Student类中含有Age属性
list.AddRange(....); //添加数据 //以下对Student集合按照其Age属性从小到大排序
list.Sort( (x, y) =>
{
if (x.Age < y.Age)
{
return -1;
}
else if (x.Age > y.Age)
{
return 1;
}
else
return 0;
}
); //或者以下更加简单的写法
list.sort((x,y)=>x.Age.CompareTo(y.Age));
https://www.cnblogs.com/talentzemin/p/3930368.html
https://blog.csdn.net/jiuzaizuotian2014/article/details/82425521
C# List.Sort与IComparer接口及Comparison委托应用于集合排序的更多相关文章
- Icomparer和Icomparable集合排序
c#中实现对象集合的排序可以使用ArrayList中的Sort()方法,而有比较才能谈排序,因为不是基本类型(如string ,int.double......等)所以.NET Framework不可 ...
- C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法
在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...
- IComparable和IComparer接口
C#中,自定义类型,支持比较和排序,需要实现IComparable接口.IComparable接口存在一个名为CompareTo()的方法,接收类型为object的参数表示被比较对象,返回整型值:1表 ...
- c# 实现IComparable、IComparer接口、Comparer类的详解
在默认情况下,对象的Equals(object o)方法(基类Object提供),是比较两个对象变量是否引用同一对象.我们要必须我自己的对象,必须自己定义对象比较方式.IComparable和ICom ...
- [0] 关于IComparable和IComparer接口和Comparer类
关于IComparable和IComparer接口 和 Comparer类 IComparable和ICompareframeworkr接口是.net 中比较对象的标准方式,这两个接口之间的区别如下: ...
- Java中使用Collections.sort()方法对数字和字符串泛型的LIst进行排序
在List的排序中常用的是Collections.sort()方法,可以对String类型和Integer类型泛型的List集合进行排序. 首先演示sort()方法对Integer类型泛型的List排 ...
- linux下sort命令使用详解---linux将文本文件内容加以排序命令
转载自:http://www.cnblogs.com/hitwtx/archive/2011/12/03/2274592.html linux下sort命令使用详解---linux将文本文件内容加以排 ...
- C#中的IComparable 和 IComparer 接口,实现列表中的对象比较和排序
借豆瓣某博主的话先对这两个接口进行一个解释: IComparable在要比较的对象的类中实现,可以比较该对象和另一个对象 IComparer在一个单独的类中实现,可以比较任意两个对象. 如果已经支持 ...
- IComparable<T> Vs. IComparer<T> System.Comparison<T>
Well they are not quite the same thing as IComparer<T> is implemented on a type that is capabl ...
随机推荐
- Linux 安装JDK配置环境(rpm安装和压缩版安装)
jdk安装 (rpm安装) jdk下载地址: https://www.oracle.com/cn/java/technologies/javase/javase-jdk8-downloads.html ...
- Git软件安装过程
Git程序安装过程 官网: https://git-scm.com/ 下载: https://git-scm.com/downloads 我的操作系统是 Windows + 64位的 https:// ...
- CSAPP:Lab1 -DataLab 超详解
写在前面 之前考研的时候csapp的书有刷过5,6遍,所以对书本知识还算比较了解.恰逢最近在学c++的时候,顺带刷一下大名鼎鼎的csapp实验. 0. 环境准备 最好准备一个纯净的Linux系统这里建 ...
- mount: /dev/sdxx already mounted or /xxxx busy解决方法
异常现象: 解决方法: 1. 輸入root的密碼,進入單用戶2. 重新掛載/目錄,使其變為可讀可寫 # mount –o rw,remount / 3. 修改/etc/fstab文件 ...
- linux7下修改主机名的方式
在基于linux发行版的众多linux kernel 3.0以上,包括rhel7,centos7等提供多种修改linux主机名的方式 1.通过编辑/etc/hostname文件 2.命令hostnam ...
- Centos 7.x系统下忘记用户登录密码,重置root密码的方法
转载的,作为一个参考保存.谢谢:https://blog.csdn.net/userpass_word/article/details/81807316 1.开机后进入以下界面,然后按Esc或者E键编 ...
- The router relies on a tree structure which makes heavy use of common prefixes, it is basically a compact prefix tree (or just Radix tree).
https://github.com/julienschmidt/httprouter/
- vscode 远程开发安装
1 首先windows 有ssh 程序 2 没有的话就直接使用git bash 登录到远程服务器开发机上 3 再开一个Git bash 执行 ssh-keygen.exe 生成秘钥 然 ...
- IO多路复用与epoll机制浅析
epoll是Linux中用于IO多路复用的机制,在nginx和redis等软件中都有应用,redis的性能好的原因之一也就是使用了epoll进行IO多路复用,同时epoll也是各大公司面试的热点问题. ...
- hashCode、equals详解
hash和hash表是什么? hash是一个函数,该函数中的实现就是一种算法,就是通过一系列的算法来得到一个hash值,这个时候,我们就需要知道另一个东西,hash表,通过hash算法得到的hash值 ...