本文介绍了在C#编程语言中实现数组排序的两种常用方法,包括使用LINQ和Array类的方法进行数据整理。适合初学者参考学习。
本段落实例讲述了C#数组排序的两种常用方法,分享给大家供大家参考。
1. 第一个例子
定义代码如下:
```csharp
#region Array数组排序1
public class Pigeon : IComparable
{
int XValue;
int YValue;
public string BatchNo { get; set; }
public int CompareTo(Pigeon other)
{
if (other == null)
throw new ArgumentNullException(比较参数为空);
```
在这个例子中,我们定义了一个名为Pigeon的类,并让该类实现IComparable接口以便对数组中的元素进行排序。在CompareTo方法中,如果传入的对象为null,则抛出异常以提示“比较参数为空”。