Collection -> 用SortedList实现排序

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<objEffic.Effic)
    return -1;
   return 0;
  }
  public int WorkHour
  {
   set
   {
    if(value<0)
     throw new ArgumentException("工作时间不能为{0}或负数");
    workHour=value;
   }
  }
  public int OutPut
  {
   set
   {
    if(value<0)
     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.GetUpperBound(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<=effics.GetUpperBound(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();
  }
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值