C#中對數組或集合進行升序或降序排序


在.net framework中,數組和集合都實現了用於排序的方法sort(),數組通過調用Array.Sort(數組名)排序,集合通過調用 集合對象.Sort()方法排序。

默認的排序是使用升序排列,並且只能針對基本的數值類型排序,因為數值類型默認實現了對兩個值比較的接口。但如果是引用類型需要排序,則此引用類型

需要實現IComparable接口,實現接口中的CompareTo()方法用於比較兩個對象。

與比較和排序有關的接口有四個:非范型版本IComparable和IComparer,范型版本IComparable<T>和IComparer<T>

IComparable接口的主要目的是在需要比較和排序的類中實現CompareTo()方法,用於定義此類的2個對象比較的時候的默認的比較規則。

IComparer接口的主要目的是使某個繼承此接口的類實現Comapre()方法,在此方法中定義兩個對象的比較規則。此類就可以作為Sort()函數的參數,用於

在排序的時候作為比較和排序的規則。

例如:如果我們有一個學生類Student,有4個字段:int ID;Name;Score;如果有一個學生數組或學生集合,此集合需要排序。

則我們要做的工作為:

1.使Student能被比較和排序,就要實現IComparable接口,在CompareTo()方法中定義默認的排序規則,例如升序。

2.如果要實現降序,則我們需要定義一個類,此類實現IComparer接口,在Compare()中定義另外一種比較和排序的規則,例如降序。

3.調用Sort()方法實現排序,此方法可以不帶參數,按默認規則排序,也可以把實現了IComparer的接口的類的對象作為參數,這樣排序時將按照

另外一種規則排序。

下面示例演示了按升序或降序對一個Student類的數組或集合進行了排序:

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

namespace ConsoleApplication1
{
    //同時實現了范型和非范型的IComparable
    public class Student:IComparable,IComparable<Student>
    {
        public int ID;
        public string Name;
        public int Score;

        public Student(int id, string name, int score)
        {
            this.ID = id;
            this.Name = name;
            this.Score = score;
        }
        public string Info()
        {
            return string.Format("學號:{0,-5}姓名:{1,-5}分數:{2}",ID,Name,Score);
        }
        int IComparable.CompareTo(object obj)
        {
            return this.Score.CompareTo((obj as Student).Score);
        }
        int IComparable<Student>.CompareTo(Student other)
        {
            return this.Score.CompareTo(other.Score);
        }
    }
    //實現了非范型的IComparer接口
    public class ReverseCompareClass : IComparer
    {

        int IComparer.Compare(object x, object y)
        {
            return ((new CaseInsensitiveComparer()).Compare(y, x));

        }
    }
    //實現了范型的IComparer接口
    public class StuReverseCompareClass : IComparer<Student>
    {
        int IComparer<Student>.Compare(Student x, Student y)
        {
            return y.Score.CompareTo(x.Score);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList stu = new ArrayList();
            //List<Student> stu = new List<Student>();
            int i;
            Random r = new Random();
            for (i = 0; i < 10; i++)
            {
                stu.Add( new Student(i,i.ToString(),r.Next(60,90)));
            }
            //如果使用非范型集合,我們就在sort()中使用非范型的比較類
            stu.Sort(new ReverseCompareClass());
            //如果使用范型集合,我們就在sort()中使用范型的比較類
            //stu.Sort(new StuReverseCompareClass());
            for (i = 0; i < 10; i++)
            {
                Console.WriteLine((stu[i] as Student).Info());
                //Console.WriteLine(stu[i].Info());
            }
            Console.Read();
        }
    }
}

 

使用范型的好處是:類型安全,效率高,因為不存在裝箱和拆箱操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM