zoukankan      html  css  js  c++  java
  • C#_IComparable实例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ComparableTest
    {
        class Program
        {
            class Employee : IComparable<Employee>
            {
                private int empID;
    
                public Employee(int empID)
                {
                    this.empID = empID;
                }
    
                public override string ToString()
                {
                    return empID.ToString();
                }
    
                public bool Equals(Employee other)
                {
                    if (this.empID == other.empID)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
    
                public int CompareTo(Employee rhs)
                {
                    return this.empID.CompareTo(rhs.empID);
                }
            }
    
    
            static void Main(string[] args)
            {
                List<Employee> le = new List<Employee>();
                Random random = new Random();
    
                for (int i = 0; i < 5;i++ )
                {
                    le.Add(new Employee(random.Next(10)+100));
                }
    
                for (int i = 0; i<le.Count; i++)
                {
                    Console.Write(le[i].ToString()+",");
                }
    
                Console.WriteLine();
                Console.WriteLine("after sort");
    
                le.Sort();
                for (int i = 0; i < le.Count; i++)
                {
                    Console.Write(le[i].ToString() + ",");
                }
    
                Console.ReadLine();
            }
        }
    }


  • 相关阅读:
    Clever Y POJ
    Searching the String ZOJ
    DNA repair HDU
    考研路茫茫——单词情结 HDU
    DNA Sequence POJ
    病毒侵袭持续中 HDU
    C语言结构体和联合体
    c语言趣味
    c语言指针
    c语言指针难点
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720474.html
Copyright © 2011-2022 走看看