数组自定义排序:IComparable和IComparer接口
首先先说一下IComparable和IComparer的区别,前者必须在实体类中实现,后者可以单独出现在一个排序类中,即此类只包含一个compare方法。
Array类使用快速算法对数组中的元素进行排序。sort()方法需要数组中的元素实现IComparable接口,下例演示了一个自定义的排序:
class Program
{
static void Main(string[] args)
{
Person[] persons ={
new Person{firstName ="Dsldkjf",lastName="Agjkl"},
new Person{firstName ="gpok",lastName="gjys"},
new Person{firstName ="ugkdf",lastName="Agjkl"},
new Person{firstName ="idfkd",lastName="6dfbh"}
}; foreach (var p in persons)
{
Console.WriteLine(p);
}
Console.WriteLine("---------------------------------------------");
Array.Sort(persons);
foreach (var p in persons)
{
Console.WriteLine(p);
}
Console.ReadKey();
}
} class Person : IComparable<Person>
{
public string firstName=string.Empty;
public string lastName = string.Empty; public int CompareTo(Person other)
{
if (other == null) return ;
int result = string.Compare(this.lastName, other.lastName);
if (result == )
{
result = string.Compare(this.firstName, other.firstName);
}
return result;
} public override string ToString()
{
return string.Format("{0} {1}" ,firstName,lastName);
}
}
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIsAAAChCAIAAAB4TNv4AAARy0lEQVR4nO2daVcbV5qA9ctmko6d1TZe2gkGk+nEsWPHfU5nps+cTudH5MyZpDPpJDNJp+N24thmNWAWIRCgrTbtey1aai9JJYlFQJ8zHwRYqBbdEkK+OPWc+6Goeuu9732fKlESGDsQqhogVX+24s9WELqG5TaDhe0guxNid0Pcbpjbi/B74ZYR4fbC3G6Y3w1zO8FiI1jcCuY38Vwdo1WUrCBZJZCWAxkJyUhIRkIzMppVcLKE02WcKuNkCcsqaEZGMpI/JfoTvC/BehOsL8H5E7w/KfjTIpKWkIyMZksYVcYPBkY+G2jLwMgKRqkYpWK0itFVjK7iTA1jajizv7E/6CpKVxFKDVBqgKz4yYo/W/Zlyt7mSJc9B2M9VWqOtVRpLaGs7g95JS6vxKWVuLQcl5Zj+8MVk1xR0RkRnRHRGeYXwvxCiFsIcnMEO09wcwQ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f1nFZO2hBgm3plyOT008oLdg8ZBZ9iTL4P9cqQ9pAuRvPqJtSe0t2kpwCTZzndjhuZM9qpm8ocqx0HqecUM4bJo6ho9VWu54BY0S3gxTFhRJshR6d74qQlaad4XvXAgtaQDVzYhmDHNgQ7tiHYsQ3Bjm0IdmxDsGMbgh3bEOyMobYhuBm1DUGObQhy/h9jOz/+wsjmOgAAAABJRU5ErkJggg==" alt="" />
下面实现一个带选择功能的排序:
class Program
{
static void Main(string[] args)
{
Person[] persons ={
new Person{firstName ="Dsldkjf",lastName="Agjkl"},
new Person{firstName ="gpok",lastName="gjys"},
new Person{firstName ="ugkdf",lastName="Agjkl"},
new Person{firstName ="idfkd",lastName="6dfbh"}
}; foreach (var p in persons)
{
Console.WriteLine(p);
}
Console.WriteLine("---------------------------------------------");
Array.Sort(persons,new PersonComparer(PersonComparType.lastName));
foreach (var p in persons)
{
Console.WriteLine(p);
}
Console.ReadKey();
}
} public struct Person
{
public string firstName;
public string lastName; public override string ToString()
{
return string.Format("{0} {1}", firstName, lastName);
}
}
public enum PersonComparType
{
firstName,
lastName
} public class PersonComparer : IComparer<Person>
{
private PersonComparType type;
public PersonComparer(PersonComparType type)
{
this.type = type;
}
public int Compare(Person x, Person y)
{
//假如Person是一个类的话 需要进行下面判断
//if (x ==null && y == null) return 0;
//if (x == null) return 1;
//if (y == null) return -1;
switch (type)
{
case PersonComparType.firstName:
return string.Compare(x.firstName, y.firstName);
case PersonComparType.lastName:
return string.Compare(x.lastName, y.lastName);
default:
throw new ArgumentException("unexpected compare type");
} }
}
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHkAAACnCAIAAAC6rGRBAAAReElEQVR4nO2d+VcTabrH86e1oiAossgqi7iwuNsz1x7Hvn3undvXmdM9nu7b7dbtbW1HcENAEEEwEGMCIYRAQvZ9qWyVqixkJSF4zvyArYGq901VkUo4WJ9TP9Spet7n+T7fKpJaSBVvRKwdEWuHxZohoWpQoHg+JX82Of94XNI3Kv7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v6SCtihnoLI9pqXaz9l225iiWLpYYUtXnOwCOd14eC8Lhyc14Vjyro+Zf10fM3BInxzhm/OOEPrrWf+Wmwtu50JQ2rCkLIHMy2nrxRby25nTBMf08RteLqZe+cJ2wwrw8PKsAVbbezkfi/DMv3zWP88ZvQl6k5wvwNjmV4R0itCdO5IzTHueaos8xvf+hvfqnaGD7eezR3NsR1uj+vvjBuW7cGDR7n37bLMTyPqn0Y0Cite3tQjBFBsjbuF6wOK6wPKRTNW1rD1f905r/PMd08Wvnsqlxv9pfWdnNfs8r+9s9f65mQG3/4jpziv2eW/7wv/9vu7eZ2npJb7/2uW+fqXyf+8y5/TIHurO4qtZbdz5ebo1dtjsyrHnqr2YmvZ7Vz+v6Gvfh6ZWbZ9UdlWbC27nH8DZjVMZbWKkfwAAAAASUVORK5CYII=" alt="" />
数组自定义排序:IComparable和IComparer接口的更多相关文章
- 对象数组自定义排序--System.Collections.ArrayList.Sort()
使用System.Collections.ArrayList.Sort()对象数组自定义排序 其核心为比较器的实现,比较器为一个类,继承了IComparer接口并实现int IComparer.Com ...
- php中数组自定义排序
php中数组自定义排序方法有很多,现在只提usort();不会保留原有键名. unsort调用方法就是unsrot($arr,func); 注意: 如果func是写在当前类中的话,那么调用的方式是 u ...
- [0] 关于IComparable和IComparer接口和Comparer类
关于IComparable和IComparer接口 和 Comparer类 IComparable和ICompareframeworkr接口是.net 中比较对象的标准方式,这两个接口之间的区别如下: ...
- C#中的IComparable 和 IComparer 接口,实现列表中的对象比较和排序
借豆瓣某博主的话先对这两个接口进行一个解释: IComparable在要比较的对象的类中实现,可以比较该对象和另一个对象 IComparer在一个单独的类中实现,可以比较任意两个对象. 如果已经支持 ...
- 对象的比较与排序:IComparable和IComparer接口
IComparable和ICompare 接口是.net framework 中比较对象的标准方式,这两个接口提供一个返回值类似(大于0 等于0 小于0)的比较方法,二者区别如下: . ICompar ...
- 比较和排序 IComparable And IComparer
1.List<Student>默认排序 为类创建默认排序实现IComparable,此代码的实现为年龄升序 using System; using System.Collections.G ...
- IComparable和IComparer接口
C#中,自定义类型,支持比较和排序,需要实现IComparable接口.IComparable接口存在一个名为CompareTo()的方法,接收类型为object的参数表示被比较对象,返回整型值:1表 ...
- c# 实现IComparable、IComparer接口、Comparer类的详解
在默认情况下,对象的Equals(object o)方法(基类Object提供),是比较两个对象变量是否引用同一对象.我们要必须我自己的对象,必须自己定义对象比较方式.IComparable和ICom ...
- php 二维数组自定义排序
eg1:只根据一个规则进行排序,比如我下面的数组是一组满减折扣的信息,我要按照满减的金额从小到大排序 代码: <?php $arr =[ ["amount"=> 60, ...
随机推荐
- In Oracle 11g, how to change the order of the results of a sql without “order by”?(转)
oracle 11g 当sql语句中不加order by的时候,好像是按rowid的顺序返回结果的.我也看过一些相关的文档,oracle的官方意思就是不加order by,就不保证输出的顺序. 那么, ...
- Atitit.软体guibuttonand面板---通信子系统(范围)-- github 采用....
Atitit.软体guibuttonand面板---通讯子系统(区)-- github 的使用.... 1. 1.注冊账户以及创建仓库 1 2. 二.在GitHub中创建项目(create a new ...
- ftk学习记录(脚本文章)
[声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 上一篇说到了对话框.今天就看看结果. watermark/2/text/aHR0cDovL2Js ...
- 使用 CXF 做 webservice 简单例子[转]
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量 ...
- H264 编解码器架构简单
看完后H264/AVC 编解码器演示,头脑是刚刚离开以下三个: 1.H264并且不明白如何指定的编解码器来实现,仅定义了一个编码视频位流的语法.和比特流进行解码,这与MPEG 类似. 2.H264而一 ...
- MySql创建一个存储过程
MySQL 存储过程是从 MySQL 5.0 新功能.存储过程的长处有一箩筐.只是最基本的还是运行效率和SQL 代码封装. 特别是 SQL 代码封装功能,假设没有存储过程,在外部程序訪问数据库时(比如 ...
- hdu Write a simple HTML Browser
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1088 对比输出 代码: #include <stdio.h> #include <s ...
- Java 实现装饰(Decorator)模式
在Java在.io反映非常多类包下是典型的装饰格局,例如: new BufferedOutputStream(OutputStream out) new BufferedInputStream(Inp ...
- Google软件构建工具Bazel
转载Google软件构建工具Bazel FAQ 本文是我的翻译,原文在这里.欢迎转载,转载请注名本文作者和原始链接注:如果想了解Bazel的原理,可以看看我之前翻译的Google Blaze原理及使用 ...
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...