1. 程式人生 > >list<自定義類>排序方法

list<自定義類>排序方法

實現 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 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();//按 方法中的邏輯來排序

list<自定義類>排序方法