下面给出部分代码,描述出如何对泛型数组排序:
//定义结构体变量
public struct WorkData
{
public int InputValue;
public int ClorType;
public WorkData(int a, int b)
{
this.InputValue = a;
this.ClorType = b;
//this.Position = c;
}
}
//数组声明
Red = Convert.ToInt32(txtRed.Text.Trim());
Blue = Convert.ToInt32(txtBlue.Text.Trim());
Yellow = Convert.ToInt32(txtYellow.Text.Trim());
Green = Convert.ToInt32(txtGreen.Text.Trim());
WorkData[] array = new WorkData[4];
array[0] = new WorkData(Red, 0);
array[1] = new WorkData(Blue, 1);
array[2] = new WorkData(Yellow, 2);
array[3] = new WorkData(Green, 3);
//定义比较方法
public class MyComparer : IComparer<WorkData>
{
#region IComparer<WorkData> 成员
int IComparer<WorkData>.Compare(WorkData x, WorkData y)
{
return y.InputValue - x.InputValue;
}
#endregion
}
//调用方法比较
Array.Sort(array, new MyComparer());