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

构建对象:

class SortGrid 
{
int indexI; int indexJ; public SortGrid(int x, int y)
{ indexI
= x; indexJ = y; } public int IndexI { get ; set ; } public int IndexJ { get ; set ;} }

编辑排序方法:

   //排序测试
    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 ();
    }

运行结果:

原文地址:https://www.cnblogs.com/imteach/p/5972216.html