using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace sortlistTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedList testList = new SortedList();
            testList.Add("wujia", 1);
            testList.Add("wujia1", 20);
            testList.Add("wujia2", 5);
            testList.Add("wujia3", 8);
            testList.Add("wujia4", 10);
            DictionaryEntry[] newArray = new DictionaryEntry[testList.Count];
            testList.CopyTo(newArray, 0);
            Array.Sort(newArray,new  myComplare2());

            
            Hashtable myTable = new Hashtable();
            myTable.Add("wujia", "ee");
            myTable.Add("wujia1", "cc");
            myTable.Add("wujia2", "dd");
            myTable.Add("wujia3", "ff");
            myTable.Add("wujia4", "zz");
            DictionaryEntry[] newArray2 = new DictionaryEntry[myTable.Count];
            myTable.CopyTo(newArray2, 0);
            Array.Sort(newArray2,new myComplare());
        }

    }
    //字符
    public class myComplare : IComparer<DictionaryEntry>
    {
        #region IComparer<DictionaryEntry> 成员
        int IComparer<DictionaryEntry>.Compare(DictionaryEntry xx, DictionaryEntry yy)
        {
            return ((new CaseInsensitiveComparer()).Compare(xx.Value, yy.Value));
        }
        #endregion
    }
    //数字 
    public class myComplare2 : IComparer<DictionaryEntry>
    {
        #region IComparer<DictionaryEntry> 成员
        public int Compare(DictionaryEntry xx, DictionaryEntry yy)
        {
            return (Convert.ToInt32(xx.Value) - Convert.ToInt32(yy.Value));
        }
        #endregion
    }
  
 
}

Posted on 2008-08-29 12:44  hesen  阅读(506)  评论(0编辑  收藏  举报