使类支持排序

  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Text;
  4 using  System.Collections;
  5
  6 namespace  ConsoleApplication1
  7 {
  8    /**//// <summary>
  9    /// 继承IComparable接口,使类可以支持排序,排序的算法在Array类中
 10    /// </summary>

 11    public class Student : IComparable 
 12    {
 13        private string m_Name;
 14        private int m_Score;
 15
 16        public Student() { }
 17
 18        public Student(string name, int score) 
 19        {
 20            this.m_Name = name;
 21            this.m_Score = score;
 22        }

 23
 24        public string Name
 25        {
 26            get return m_Name; }
 27            set { m_Name = value; }
 28        }

 29
 30        public int Score
 31        {
 32            get return m_Score; }
 33            set { m_Score = value; }
 34        }

 35
 36        IComparable 成员#region IComparable 成员
 37
 38        //比较分数
 39        public int CompareTo(object obj)
 40        {
 41            if (obj.GetType() != this.GetType()) 
 42            {
 43                throw new ArgumentException();
 44            }

 45            if (this.Equals(obj)) 
 46            {
 47                return 0;
 48            }

 49            Student s = (Student)obj;
 50            if (s.Score == this.Score)
 51            {
 52                return 0;
 53            }

 54            else if (this.Score > s.Score)
 55            {
 56                return 1;
 57            }

 58            else 
 59            {
 60                return -1;
 61            }

 62        }

 63
 64        #endregion

 65      
 66    }

 67
 68    public class StudentSortByName :  IComparer 
 69    {
 70
 71        IComparer 成员#region IComparer 成员
 72
 73        //以学生名称排序
 74        public int Compare(object x, object y)
 75        {
 76            if (!(x is Student) || !(y is Student))
 77            {
 78                throw new ArgumentException();
 79            }

 80            if (x.Equals(y)) 
 81            {
 82                return 0;
 83            }

 84            Student s1 = (Student)x;
 85            Student s2 = (Student)y;
 86
 87            return string.Compare(s1.Name, s2.Name);
 88        }

 89
 90        #endregion

 91    }

 92
 93    class Program
 94    {
 95        static void Main(string[] args)
 96        {
 97            Student[] students = new Student[10];
 98            Random rd = new Random();
 99            for (int i = 0; i < 10; i++
100            {
101                students[i] = new Student("Student" + i.ToString(), rd.Next(100));
102            }

103            Console.WriteLine("分数从小到大排序");
104            Array.Sort(students);
105            ShowStudents(students);
106
107            Console.WriteLine("分数从大到小排序");
108            Array.Reverse(students);
109            ShowStudents(students);
110
111            Console.WriteLine("以学生名称排序");
112            Array.Sort(students, new StudentSortByName());
113            ShowStudents(students);
114            Console.ReadLine();
115
116
117        }

118
119        private static void ShowStudents(Student[] students)
120        {
121            foreach (Student s in students)
122            {
123                Console.WriteLine("Name :" + s.Name + " Score :" + s.Score.ToString());
124            }

125        }

126    }

127}

128

转载于:https://www.cnblogs.com/ghx88/archive/2007/09/02/879302.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值