用SortedList實現排序

來源:互聯網
上載者:User

  using System;
  using System.Collections;
  namespace 集合的比較和排序
  {
  
  public class Efficience:IComparable
  {
  private int workHour;
  private int outPut;
  int IComparable.CompareTo(Object obj)
  {
  if(obj==null)
  throw new ArgumentException("比較對象不可為空");
  if(!obj.GetType().Equals(this.GetType()))
  throw new ArgumentException("比較的兩者類型不同");
  Efficience objEffic=(Efficience)obj;
  if(this.Effic>objEffic.Effic)
  return 1;
  if(this.Effic  return -1;
  return 0;
  }
  public int WorkHour
  {
  set
  {
  if(value  throw new ArgumentException("工作時間不能為{0}或負數");
  workHour=value;
  }
  }
  public int OutPut
  {
  set
  {
  if(value  throw new ArgumentException("工作產出不能為負數");
  outPut=value;
  }
  }
  public float Effic
  {
  get
  {
  return (float)outPut/(float)workHour;
  }
  }
  }
  class Class1
  {
  [STAThread]
  static void Main(string[] args)
  {
  Random rand=new Random();
  Efficience[] effics=new Efficience[5];
  string[] persons={"Xiaohua","Diana","YanYan","ZhuLin","LiXin"};
  Console.WriteLine("產生的 Effics 數組");
  //Console.WriteLine("effics.GetLowerBound(0)={0},effics.GetUpperBound(0)={1}",effics.GetLowerBound(0),effics.GetUpperBound(0));
  for(int i=effics.GetLowerBound(0);i  {
  effics[i]=new Efficience();
  effics[i].WorkHour=rand.Next()%24;
  effics[i].OutPut=rand.Next()%1000;
  Console.WriteLine("Person={0},Effic={1}",persons[i],effics[i].Effic);
  }
  
  SortedList sortedList=new SortedList();
  for(int i=effics.GetLowerBound(0);i  {
  sortedList.Add(effics[i],persons[i]);
  }
  Console.WriteLine("從 sortedList 中讀取內容");
  foreach(Efficience effic in sortedList.GetKeyList())
  {
  Console.WriteLine("Person={0},Effic={1}",sortedList[effic],effic.Effic);
  }
  Console.Read();
  }
  }
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.