一个数组
Item[] arr
里面的对象是Item类型的
Item有int类型的i属性 有string类型的s属性
怎么根据Item的i或Item的s来排序

解决方案 »

  1.   


       protected void Page_Load(object sender, EventArgs e)
            {
                Item o1 = new Item(1,"a");
                Item o2 = new Item(3, "b");
                Item o3 = new Item(5, "b");
                Item o4 = new Item(2, "b");
                Item o5 = new Item(4, "b");
                Item[] aryItem = new Item[] { o1, o2, o3, o4, o5 };
                myComparer cp = new myComparer();
                Array.Sort(aryItem, cp);            for (int i = 0; i < aryItem.Length; i++)
                {                Response.Write(aryItem[i].i.ToString() + "<BR>");
                }        }  public class myComparer : IComparer
        {        #region IComparer 成员        public int Compare(object x, object y)
            {
                Item oItem1 = (Item)x;
                Item oItem2 = (Item)y;
                return oItem1.i - oItem2.i;
            }        #endregion
        }
        public class Item
        {
            public int i;
            public string s;
            public Item(int i, string s)
            {
                this.i = i;
                this.s = s;
            }
        }