键值可以重复排列的链表

 1 //SortedList<TKey, TValue>这个链表是一个自动按键值排列的链表 使用foreach遍历时 类型为KeyValuePair<TKey, TValue>  
 2 //但该链表有一个问题是键值不能重复 这边由于需要键值可以重复 所以必须要定义一个类派生自IComparer<KeyType> 尖括号内为键值的类型  
 3   
 4 //IComparer接口中有一个方法Compare 比较两个对象并返回一个值 表示小于 等于或大于另一个对象   
 5 //在这里我们需要做的就是重写这个方法 使其返回值不等于0 此处用int作为示例  
 6 public class MySort : IComparer<int>  
 7 {  
 8     public int Compare (int x, int y)  
 9     {  
10         int result = x - y;  
11         if(result <= 0)   
12         {  
13             result = -1;  
14         }  
15         return 1;  
16     }  
17 }  
18   
19 void func ()  
20 {  
21     //这样定义的list就是键值可以重复的排序链表  
22     SortedList<int, string> list = new SortedList<int, string>(new MySort());  
23 } 
24  

 

posted @ 2013-06-07 16:47  Clover-Z  阅读(205)  评论(0编辑  收藏  举报