list<自定義類>排序方法
阿新 • • 發佈:2017-11-20
實現 onerror var div lis rabl bsp 邏輯 int
類實現接口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 stringdataType; 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();//按 方法中的邏輯來排序
list<自定義類>排序方法