用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
  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
    评论
在 VB.NET 中,可以使用 `SortedList` 对象来存储键/值对,并且可以使用 `Comparer` 对象来自定义排序规则。 以下是一个示例,演示如何使用自定义排序规则对 `SortedList` 进行排序: ```vb ' 创建一个自定义排序规则类 Public Class MyComparer Implements IComparer ' 实现 Compare 方法,用于比较两个对象的大小 Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _ Implements System.Collections.IComparer.Compare ' 将 x 和 y 转换为需要比较的类型 Dim xValue As String = CType(x, KeyValuePair(Of String, Integer)).Key Dim yValue As String = CType(y, KeyValuePair(Of String, Integer)).Key ' 使用 String.Compare 方法进行比较,并返回比较结果 Return String.Compare(xValue, yValue) End Function End Class ' 使用自定义排序规则对 SortedList 进行排序 Dim mySortedList As New SortedList(New MyComparer()) ' 添加键/值对 mySortedList.Add("B", 2) mySortedList.Add("C", 3) mySortedList.Add("A", 1) ' 遍历 SortedList,输出键/值对 For Each kvp As KeyValuePair(Of String, Integer) In mySortedList Console.WriteLine(kvp.Key & " - " & kvp.Value) Next ``` 在上面的示例中,我们创建了一个名为 `MyComparer` 的自定义排序规则类,并实现了 `IComparer` 接口中的 `Compare` 方法。在 `Compare` 方法中,我们使用 `String.Compare` 方法对键进行比较,并返回比较结果。 在创建 `SortedList` 对象时,我们将 `MyComparer` 对象作为参数传递,这样就会使用我们自定义的排序规则对键进行排序。 最后,我们遍历 `SortedList` 中的键/值对,输出它们的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值