ArryList数组中我存放了如下数据
'10,55,12,14,40'
如何对ArryList数组进行由小到大,或由大到小的排序,我记得好象有这个排序的方法,但是没找到

解决方案 »

  1.   

    ArrayList可以对元素进行排序,但是要实现ICompare接口
    楼主参数一下:
    //实现ICompare接口
    public class MySort : IComparer  
    {
        // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
        int IComparer.Compare(Object x, Object y)  
        {
            int i = Convert.ToInt32(x);
            int j = Convert.ToInt32(y);        if (i > j)
                return 1;
            else if (i < j)
                return -1;
            else return 0;
        }
    }        ArrayList a = new ArrayList();
            a.Add(10);
            a.Add(55);
            a.Add(12);
            a.Add(14);
            a.Add(40);        IComparer compare = new MySort();        a.Sort(compare);        foreach (object o in a)
            {
                Console.WriteLine(o.ToString());
            }
      

  2.   

    1. 页面后台代码:protected void Page_Load(object sender, EventArgs e)
        {
            ExtendPerson scott = new ExtendPerson("Scott", "Hanselman");
            ExtendPerson bill = new ExtendPerson("Bill", "Evjen");
            ExtendPerson srini = new ExtendPerson("Srinivasa", "Sivakumar");        ArrayList people = new ArrayList();
            people.Add(scott);
            people.Add(bill);
            people.Add(srini);        Response.Write("We used foreach:<BR/>");
            foreach (ExtendPerson p in people)
            {
                Response.Write(p.FullName + "<BR/>");
            }        Response.Write("<br>Sort...<BR><br>");
            people.Sort();        Response.Write("We used foreach:<BR/>");
            foreach (ExtendPerson p in people)
            {
                Response.Write(p.FullName + "<BR/>");
            }        ExtendPerson scott2 = new ExtendPerson("Scott", "Hanselman");
            int indexOfScott2 = people.IndexOf(scott2);
            Response.Write("<br><br>Scott #2 is at " + indexOfScott2 + "<BR/>");        int indexOfEquivalentScott = people.BinarySearch(scott2);
            Response.Write("An Equivalent Scott is at " + indexOfEquivalentScott +
            "<BR/>");        Response.Write("<br>We used a for loop.<BR/>");
            for (int i = 0; i < people.Count; i++)
            {
                Response.Write(((ExtendPerson)people[i]).FullName + "<BR/>");
            }
        }
    2.实体类public class Person
    {
        private string FirstName;
        private string LastName;    public Person(string first, string last)
        {
            FirstName = first;
            LastName = last;
        }    public string FullName
        {
            get
            {
                return FirstName + " " + LastName;
            }
        }
    }
      

  3.   

    可以用SortedList 能对键排序
      

  4.   

    提示下把你的要排序数据做为键值示例代码如下:System.Collections.SortedList sl=new SortedList();
    sl.Add(10,1);
    sl.Add(20,2);
    sl.Add(14,3);
    string s="";
    foreach(System.Collections.DictionaryEntry de in sl)
    {
    s+=de.Value.ToString()+"<br>";

    }
    Response.Write(s);1
    3
    2