net中是用一个Compare函数来对添加到集合中的键进行比较的,只要这个函数返回的值不等于0,就可以正常向集合添加数据
using System;
using System.Collections;
namespace testSortedList
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
//声明可重复键的链表
SortedList sl = new SortedList(new MySort());
sl.Add(333, 333);
sl.Add(111, 111);
sl.Add(222, 222);
sl.Add(111, 112);
PrintList(sl);
Console.ReadLine();
}
private static void PrintList(SortedList sl)
{
for (int i = 0; i < sl.Count; i++)
{
Console.WriteLine("{0}\t{1}",
sl.GetKey(i), sl.GetByIndex(i));
}
}
}
//继承接口IComparer
public class MySort : IComparer
{
#region IComparer 成员
public int Compare(object x, object y)
{
return -1;
//排序
//int iResult = (int)x - (int)y;
//if(iResult == 0) iResult = -1;
//return iResult;
}
#endregion
}
}
重写compare之后,SortList中ContainKey和其它方法都没法用了
但是问题是如果键值重复了,怎么办,下篇继续
参考http://www.lubanren.com/weblog/post/184.html