您的位置:首页 > 编程语言 > C#

C#对象内部属性排序测试

2016-10-18 10:01 471 查看
构建对象:

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 ();
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: