您现在的位置是:首页 >

js根据数组对象中的一个值排序 C#数组排序与对象大小比较

火烧 2022-08-31 00:06:23 1070
C#数组排序与对象大小比较 我们将介绍C#数组排序与对象大小比较 包括一些实例代码以及ICom ara le ICom ara le和ICom arer三大接口的用法   从个小例子开始  i t[]

C#数组排序与对象大小比较  

        我们将介绍C#数组排序与对象大小比较 包括一些实例代码以及IComparable IComparable和IComparer三大接口的用法

  从个小例子开始

  int[] intArray = new int[]{ } Array Sort(intArray) Array ForEach<int>(intArray (i)=>Console WriteLine(i)) 这个例子定义了一个int数组 然后使用Array Sort(arr)静态方法对此数组进行排序 最后输出排序后的数组 以上例子将毫无意外的依次输出

  为什么Array的Sort方法可以正确的对int数组进行排序呢 我们自定义类可以吗?试试看 如下代码

  public class Student { public int Age { get set } public string Name { get set } public int Score { get set } } static void Main(string[] args)

  { Student[] students = new Student[]{ new Student(){Age = Name= 张三 Score= } new Student(){Age = Name= 李四 Score= } new Student(){Age = Name= 王五 Score= } new Student(){Age = Name= 赵六 Score= } new Student(){Age = Name= 司马 Score= } } Console WriteLine( ——默认排序输出—— ) Array Sort(students) Array ForEach<Student>(students (s)=>Console WriteLine(string Format( { }{ }岁了 他的分数是{ } s Name s Age s Score))) Console Read() }我们定义了Student类然后同样对他的数组进行排序 程序正确的编译通过 但是运行出错 运行时抛出了异常 System InvalidOperationException{ Failed to pare o elements in the array } 这个异常的InnerException是ArgumentException{ At least one object must implement IComparable } 运行时异常说明 我们要使用Array Sort(arr)静态方法 必须得保证数组中有一个元素实现IComparable接口 既然如此我们就让Student类实现IComparable接口

  public class Student IComparable { public int Age { get set } public string Name { get set } public int Score { get set } /// <summary> /// 实现IComparable接口 用Age做比较/// </summary> /// <param name= obj >比较对象</param> /// <returns>比较结果</returns> public int CompareTo(object obj)

  { if (obj is Student)

js根据数组对象中的一个值排序 C#数组排序与对象大小比较

  { return Age CompareTo(((Student)obj) Age) } return }在Student类中实现了IComparable接口 在CompareTo方法中比较Student的Age属性 这一次再次编译运行 程序正常的输出了按照年龄排序的Student数组

  假如说我们要对Student的Score属性进行排序该怎么办呢? Student类实现的IComparable接口只能按照一种属性排序呀

  这个是很容易实现的 net的类库开发者早为我们准备了另一个接口IComparer<T>接口用来实现比较类型T的两个实例 如下StudentScoreComparer类实现了对Student按照Score属性比较的IComparer<Student>

  public class StudentScoreComparer IComparer<Student> { public int Compare(Student x Student y)

  { return x Score CompareTo(y Score) }现在我们可以使用下面代码对Student数组按照Score属性进行排序

  Console WriteLine( ——按分数排序输出—— )

  Array Sort(students new StudentScoreComparer())

  Array ForEach<Student>(students (s) => Console WriteLine(string Format( { }{ }岁了 他的分数是{ } s Name s Age s Score)))

  不过一个简单的按照Score属性排序 再定义一个类是不是有点大题小作呀 有没有更好的办法呢?当然有 为我们准备了比较对象大小的委托Comparison<T>我们可以使用拉姆达表达式或者匿名委托直接排序 如下代码实现

  Console WriteLine( ——按分数排序输出—— ) Array Sort(students (s s ) => s Score CompareTo(s Score)) Array ForEach<Student>(students (s) => Console WriteLine(string Format( { }{ }岁了 他的分数是{ } s Name s Age s Score))) 完整代码示例如下

  using System using System Collections Generic using System Linq using System Text namespace SortingInCSharp { class Program { public class Student IComparable { public int Age { get set } public string Name { get set } public int Score { get set } /// <summary> /// 实现IComparable接口 用Age做比较/// </summary> /// <param name= obj >比较对象</param> /// <returns>比较结果</returns> public int CompareTo(object obj)

  { if (obj is Student)

  { return Age CompareTo(((Student)obj) Age) } return } static void Main(string[] args)

  { Student[] students = new Student[]{ new Student(){Age = Name= 张三 Score= } new Student(){Age = Name= 李四 Score= } new Student(){Age = Name= 王五 Score= } new Student(){Age = Name= 赵六 Score= } new Student(){Age = Name= 司马 Score= } } Console WriteLine( ——默认排序输出—— ) Array Sort(students) Array ForEach<Student>(students (s) => Console WriteLine(string Format( { }{ }岁了 他的分数是{ } s Name s Age s Score))) Console WriteLine( ——按分数排序输出—— ) Array Sort(students new StudentScoreComparer()) Array ForEach<Student>(students (s) => Console WriteLine(string Format( { }{ }岁了 他的分数是{ } s Name s Age s Score))) Console WriteLine( ——按分数排序输出—— ) Array Sort(students (s s ) => s Score CompareTo(s Score)) Array ForEach<Student>(students (s) => Console WriteLine(string Format( { }{ }岁了 他的分数是{ } s Name s Age s Score))) Console Read() } public class StudentScoreComparer IComparer<Student> { public int Compare(Student x Student y)

  { return x Score CompareTo(y Score) }总结

lishixinzhi/Article/program/net/201311/11369  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码