構建對象:
class SortGrid { int indexI; int indexJ; public SortGrid(int x, int y) { indexI = x; indexJ = y; } public int IndexI { get { return indexI; } set { indexI = value; } } public int IndexJ { get { return indexJ; } set { indexJ = value; } } }
編輯排序方法:
//排序測試 void SortTest () { Debug.Log ("C#對象內部屬性排序測試:"); SortGrid sg2 = new SortGrid (5, 4); SortGrid sg1 = new SortGrid (1, 6); SortGrid sg3 = new SortGrid (3, 2); List<SortGrid> lsg = new List<SortGrid> (); lsg.Add (sg1); lsg.Add (sg2); lsg.Add (sg3); lsg.Sort (delegate(SortGrid x, SortGrid y) { return x.IndexI.CompareTo(y.IndexI); }); foreach (var item in lsg) { Debug.Log("indexI:" + item.IndexI); } lsg.Sort (delegate(SortGrid x, SortGrid y) { return y.IndexI.CompareTo(x.IndexI); }); foreach (var item in lsg) { Debug.Log("indexI:" + item.IndexI); } }
調用方法:
// Use this for initialization void Start () { SortTest (); }
運行結果: