c#中实现对象集合的排序可以使用ArrayList中的Sort()方法,而有比较才能谈排序,因为不是基本类型(如string ,int.double......等)所以.NET Framework不可能一一制定他们的比较规则,那么则需要程序员自行制定,而比较规则的制定就需要通过继承这两个接口>之一来实现。制定了比较规则后则才可以用以下两种方式之一调用排序:

(1)ArrayList实例.Sort(); // IComparable

(2)ArrayList实例.Sort(实现Icomparer接口的类); // Icomparer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace Demo3
{
class Program
{
static void Main(string[] args)
{
//新建集合people用来存放person实例
ArrayList people = new ArrayList();
//建立4个person实例
Person person1 = new Person("Jone", 18);
Person person2 = new Person("Tom", 20);
Person person3 = new Person("Lily", 15);
Person person4 = new Person("July", 25);
//将实例添加到people集合中
people.Add(person1);
people.Add(person2);
people.Add(person3);
people.Add(person4);
//输出原来序列
Console.WriteLine("原来序列:");
foreach (Person person in people)
{
Console.WriteLine("person name: {0} age:{1}", person.Name, person.Age);
}
//用实现Icomparable进行排序
people.Sort();
//用实现Icomparable的方法输出排序后的序列
Console.WriteLine("按年龄排序后的序列:");
foreach (Person person in people)
{
Console.WriteLine("person name: {0} age:{1}", person.Name, person.Age);
}
//用实现Icomparer的方法进行排序
people.Sort(PersonComparer.Default);
//用实现Icomparer的方法输出排序后的序列
Console.WriteLine("按名称排序后的序列:");
foreach (Person person in people)
{
Console.WriteLine("person name: {0} age:{1}", person.Name, person.Age);
}
Console.ReadKey();
}
} public class Person : IComparable
{
/// <summary>
/// 两个私有字段:
/// 人物姓名;
/// 人物年龄;
/// </summary>
private string name;
private int age; /// <summary>
/// 构造函数
/// </summary>
public Person(string myname, int myage)
{
name = myname;
age = myage;
} /// <summary>
/// 两个共有属性:
/// 分别对应两个私有字段;
/// </summary>
public string Name
{
set
{
name = value;
}
get
{
return name;
}
}
public int Age
{
set
{
age = value;
}
get
{
return age;
}
} public int CompareTo(object myobject)
{
if (myobject is Person)//用is运算符判断要比较的对象是否是Person对象
{
//如果是用as运算符进行对象转换,返回年龄比较结果(一个整数,表示两者差)
Person myperson = myobject as Person;
return this.Age - myperson.Age;
//return myperson.Age - this.Age;
}
else
{
//如果不是,抛出异常
throw new ArgumentException("Object to compare to is not a Person Object");
}
}
} public class PersonComparer : IComparer
{
//静态字段,方便使用,没有也可,调用方法会变
public static IComparer Default = new PersonComparer();
public int Compare(object myperson1, object myperson2)
{
//用is运算符判断要比较的对象是否都是Person对象
if (myperson1 is Person && myperson2 is Person)
{
//如果是,调用.Net Framework已经实现好的能比较基本类型的函数:Comparer.Default.Compare
//(要用using System.Collections;)
return Comparer.Default.Compare(((Person)myperson1).Name, ((Person)myperson2).Name);
}
else
{
//如果不是抛出异常
throw new ArgumentException("One or both objects to compare are not Person objects.");
}
}
}
}

方法论:读书加上网查询相关资料,能够更好的理解知识点。

Icomparer和Icomparable集合排序的更多相关文章

  1. C# List.Sort与IComparer接口及Comparison委托应用于集合排序

    C#里List.Sort的用法 using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  2. .NET Framework System.Array.Sort 数组类,加深对 IComparer、IComparable 以及泛型委托、匿名方法、Lambda 表达式的理解

    本文内容 自定义类 Array.Sort 参考资料 System.Array.Sort 有很多对集合的操作,比如排序,查找,克隆等等,你可以利用这个类加深对 IComparer.IComparable ...

  3. .Net中集合排序的一种高级玩法

    背景: 学生有名称.学号, 班级有班级名称.班级序号 学校有学校名称.学校编号(序号) 需求 现在需要对学生进行排序 第一排序逻辑 按学校编号(序号)排列 再按班级序号排列 再按学生学号排列 当然,在 ...

  4. 通过写一个Demo展示C#中多种常用的集合排序方法

    不多说,程序很简单,就是将集合中的数据进行排序,但使用到的知识点还是比较多的,大牛勿喷,谨献给初学者!直接上程序吧! namespace Demo { /// <summary> /// ...

  5. Java比较器对数组,集合排序一

    数组排序非常简单,有前辈们的各种排序算法,再加上Java中强大的数组辅助类Arrays与集合辅助类Collections,使得排序变得非常简单,如果说结合比较器Comparator接口和Collato ...

  6. ArrayList集合排序

    using System;using System.Collections;using System.Collections.Generic;using System.Text; namespace ...

  7. 【Java进阶】---map集合排序

    map集合排序         这篇文章讲的不仅仅是map排序,比如把对象按某一属性排序,它都可以解决这些问题.   比如,有N个对象,每个对象有个属性就是成绩,成绩分:优秀,良好,合格.那我们如何按 ...

  8. CopyOnWriteArrayList集合排序异常问题

    1.集合自定义排序实现 对List集合的自定义排序想必大家都知道要使用如下的方式,通过实现Comparator接口并实现compare方法来实现. /** * * @方法名 changeChain * ...

  9. 二维码扫描&集合排序

    一.二维码扫描机制 二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的:在代码编制上巧妙地利用构 ...

随机推荐

  1. 11.string容器

    #include <iostream> //string的本质也是容器 #include <string> #include <cstdlib> using nam ...

  2. 重温前端基础之-css浮动之怪异现象

    其实,两种情况中box2的内容都是围绕着浮动元素box1来的. 因为:浮动元素会覆盖块元素,但块元素里的内容(内联盒)不会被覆盖,而是围绕着浮动盒. 将box1的背景色去掉,真相就大白了: 此时发现, ...

  3. hadoop ha

    https://blog.csdn.net/daydayup_668819/article/details/70815335 https://www.jianshu.com/p/8a6cc2d7206 ...

  4. POJ-2142 The Balance 扩展欧几里德(+绝对值和最小化)

    题目链接:https://cn.vjudge.net/problem/POJ-2142 题意 自己看题吧,懒得解释 思路 第一部分就是扩展欧几里德 接下来是根据 $ x=x_0+kb', y=y_0- ...

  5. NetHogs---按进程或程序实时统计网络带宽使用率。

    NetHogs是一个开源的命令行工具(类似于Linux的top命令),用来按进程或程序实时统计网络带宽使用率. 来自NetHogs项目网站: NetHogs是一个小型的net top工具,不像大多数工 ...

  6. 移动端(手机端)页面自适应解决方案—rem布局篇

    移动端(手机端)页面自适应解决方案-rem布局 假设设计妹妹给我们的设计稿尺寸为750 * 1340.结合网易.淘宝移动端首页html元素上的动态font-size属性.设计稿尺寸.前端与设计之间协作 ...

  7. caioj 1413 动态规划4:打鼹鼠

    记住一定要区分n和m分别代表什么,我已经因为这个两道题浪费很多时间了 然后这个道题有点类似最长上升子序列n平方的做法,只是判断的条件不同而已 #include<cstdio> #inclu ...

  8. string.split 应用

    采用string.split将字符串依据分隔符,转换成字符串数组,生成的字符串数组中会包含空数组元素,需要通过StringSplitOptions.RemoveEmptyEntries参数选项去除. ...

  9. 网络编程select函数

    select函数的作用: 在编程的过程中,经常会遇到许多阻塞的函数,好像read和网络编程时使用的recv, recvfrom函数都是阻塞的函数,当函数不能成功执行的时候,程序就会一直阻塞在这里,无法 ...

  10. 一个最不可思议的MySQL死锁分析

    1    死锁问题背景    1 1.1    一个不可思议的死锁    1 1.1.1    初步分析    3 1.2    如何阅读死锁日志    3 2    死锁原因深入剖析    4 2. ...