自定义排序(Icompare)

  static void Main(string[] args)
        {
            #region MyRegion
           
           
            

            #endregion

            ArrayList arry = new ArrayList() { 
            new Person(){Name="Li Si",Age=30},
            new Person(){Name="Zhang San",Age=20},
            };
            arry.Sort(new AutoCompareAsc());
            for (int i = 0; i < arry.Count; i++)
            {
                Console.WriteLine(((Person)arry[i]).Age);
            }      
            Console.ReadKey();              
        }
      /// <summary>
      /// 两个值的升序排列
      /// </summary>
        class AutoCompareAsc:IComparer
        {
            public int Compare(object x,object y)
            {
                Person p = x as Person;
                Person p1 = y as Person;
                if (p == null && p1 == null)
                {
                    throw new ArgumentException();
                }
                else
                {
                    return p.Age - p1.Age;
                }
            }
        }
      /// <summary>
      /// 两个值之间的降序排列
      /// </summary>
        class AutoCompareDesc : IComparer
        {
            public int Compare(object x ,object y)
            {
                Person p = x as Person;
                Person p1 = y as Person;
                if (p == null && p1 == null)
                {
                    throw new ArgumentException();
                }
                else
                {
                    return p1.Age - p.Age;
                }
            }
        }
        class AutoCompareNameAsc:IComparer
        {
            public int Compare(object x ,object y)
            {
                Person p = x as Person;
                Person p1 = y as Person;
                if (p == null && p1 == null)
                {
                    throw new ArgumentException();
                }
                else
                {
                    return p.Name.Length - p1.Name.Length;
                }
            }
        }
View Code

利用Sort方法的第二个参数进行排序;需重写接口Icmpare 的Compare方法可自定义排序

C# 中  数组,ArryList  List<> 三者之间的区别


数组

   数组是C#语言中出现最早。可以在内存中连续存储,索引速度快,赋值与修改值很简单。

缺点:数组分配在一个连续的存储空间上,因此在分配空间上必须确认其大小,空间的连续也导致了存储的效率低下,插入和删除效率也比较低。增加与删除元素时需要移动大量元素。

string【】s=new string[3];//定义数组

s[0]="ss";//赋值

s[1]="sss";

s[2]="ssss';

s[0]="bbbbb";//修改

 

ArrayList

   针对于数组的缺点,产生了ArrayList (非泛型集合),用于数据的存储于检索的专用类,它的命名空间在System.Collections下的一部分。存储大小是按照数据的状态进行大小收缩(翻倍),使用时不用确定其大小。ArrayList 类的对象可以是任何数据类型,所以涉及到数据的装箱拆箱过程会损耗性能

ArryList list= new ArryList(); //定义非泛型集合

list.add("s");  //增加值

foreach(object item object list)  //第一种

{

console.writeLine(item.ToString()+" ");

}

for(i=0;i<list.Count;i++){Console.WriteLine(list[i]).ToString()+" "};

ArrayList list = new ArrayList();

//新增数据
list.Add("abc");
list.Add(123);

//修改数据
list[2] = 345;

//移除数据
list.RemoveAt(0);

//插入数据
list.Insert(0, "hello world");

获取元素值
object value = al[index]; //al 为 ArrayList 对象,一般需要再对 value 进行类型转换,比如:int n = (int)value;
设置元素值
al[index] = value; //al 为 ArrayList 对象,index 必须小于 Count
追加元素
int ArrayList.Add(object value) //返回添加的元素的索引
插入元素
void ArrayList.Insert(int index, object value)
删除元素
删除元素后,后面的元素会前移,但 Capacity 不会变化。
void ArrayList.Remove(object obj) //从前(索引 0)往后查找,删除找到的第一个和 obj 相同的元素
void ArrayList.RemoveAt(int index) //删除索引 index 对应的元素
void ArrayList.RemoveRange(int index, int count) //从索引 index 开始,删除 count 个元素
查找元素
int ArrayList.IndexOf(object value) //从前(索引 0)往后查找,返回找到的第一个和 obj 相同的元素的索引
int ArrayList.IndexOf(object value, int startIndex)
int ArrayList.IndexOf(object value, int startIndex, int count)
int ArrayList.LastIndexOf(object value) //从后往前(索引 0)查找,返回找到的第一个和 obj 相同的元素的索引
int ArrayList.LastIndexOf(object value, int startIndex)
int ArrayList.LastIndexOf(object value, int startIndex, int count)
View Code


List

    基于非泛型的确定在C#2.0出现了泛型集合List类 与ArryList类的大部分功能都相似,同时也继承了Ilist接口

Console.WriteLine("List Test:"); 
//声明一个整型的List 
List<int> lsTest = new List<int>(); 
lsTest.Add(7); 
lsTest.Add(5); 
lsTest.Add(1); 
lsTest.Add(3); 
string strTest=""; 
//list的排序 
lsTest.Sort(); 
//list的遍历 
foreach(int i in lsTest) 
strTest+=i.ToString()+" "; 
//格式化后输出 
Console.Write(string.Format("Out:{0} nCount:{1}n",strTest,lsTest.Count)); 
//读取下一个按键,以便让屏幕显示数据 
Console.ReadKey();

shuchuruxia:
List Test: 
Out:1 3 5 7 
Count:4
View Code

 

posted on 2017-02-05 20:32  路上有你F  阅读(263)  评论(0编辑  收藏  举报