类实现接口IComparable
//实现接口的自定义类 //此接口规定了一个返回类型int的方法, //public int CompareTo(object obj) //返回1时this排object前面,0是并列,-1时this排object后面 public class varConf:IComparable { public string name; public string deviceType; public string deviceAddress; public string dataType; public string upLimit; public string downLimit; public string onErrorValue; public string isLock; public byte[] bytes; public int CompareTo(object obj) { varConf other = obj as varConf; int result = 0; if (getValueTypeAddress(deviceAddress) > getValueTypeAddress(other.deviceAddress)) { result = 1; } if (getValueTypeAddress(deviceAddress) == getValueTypeAddress(other.deviceAddress)) { result = 0; } if (getValueTypeAddress(deviceAddress )< getValueTypeAddress(other.deviceAddress)) { result = -1; } return result; } }
List<varConf> varList = new List<varConf>();//这个是自定义类的泛型合集 varList.Sort();//按 方法中的逻辑来排序
时间: 2024-11-05 11:55:24