通过实现IComparer:
public class ReverserSort : IComparer<string>
{
private bool Asc=true;
int IComparer<KeyValueItem>.Compare(string x, string y)
{
if (Asc)
return string.Compare(x, y);
else
return string.Compare(y, x);
}
public bool bAsc
{
set { Asc = value; }
}
}
其中string类型也可以是其他类型
ReverserSort ms = new ReverserSort();
ms.Asc = false;
SortedList li = new SortedList();
Array.Sort(li, ms);
-----------------------------------------
去除SortedList的自动排序功能
1 public class MySort : IComparer
2
3 {
4 public MySort()
5 {
6 //
7 //TODO: 在此处添加构造函数逻辑
8 //
9 }
10 public int Compare(object x, object y)
11 {
12 return -1;
13
14 //排序
15 // int iResult = (int)x - (int)y;
16 // if(iResult == 0) iResult = -1;
17 // return iResult;
18
19 }
20
21 }
一下是对IComparer类的Compare()重写
然后在对SortedList类进行实例化的时候
SortedList strDiction = new SortedList(new MySort());
就消除了 SortedList的自动排序功能!
---------------------------------------------------------------------
Dictionary, SortedDictionary, SortedList 比较
- http://msdn.microsoft.com/zh-cn/library/5z658b67%28VS.80%29.aspx
- 下表列举两个排序的列表类与 SortedDictionary 类之间的一些区别。
SortedList 非泛型类和 SortedList 泛型类 | SortedDictionary 泛型类 |
---|---|
返回键和值的属性是有索引的,从而允许高效的索引检索。 | 无索引的检索。 |
检索的运算复杂度为 O(log n)。 | 检索的运算复杂度为 O(log n)。 |
插入和移除的运算复杂度一般为 O(n);但是,对于已经处于排序顺序的数据,插入操作的运算复杂度为 O(1),因此每个元素都被添加到列表的末尾。(这假设不需要调整大小。) | 插入和移除的运算复杂度为 O(log n)。 |
比 SortedDictionary 使用更少的内存。 | 比 SortedList 非泛型类和 SortedList 泛型类使用更多内存。 |
http://www.cnblogs.com/samcn/archive/2009/03/12/1409725.html
如果只是当作索引使用, 请用 Dictionary.
如果需要查找最小的几个元素, 或者需要按顺序遍历元素, 就用 SortedDictionary.
如果输入/删除的元素是基本增序的, 或者访问次数远多于修改次数, 或者需要访问第 k 大的元素, 或者对内存吝啬得 BT 的情况, 用 SortedList 吧.
微软似乎也很吝啬, SortedDictionary 居然只支持增序 (默认的比较器), 如果要降序的话, 我们得自己写一个比较器.
http://www.cnblogs.com/Charles2008/archive/2008/01/29/1056411.html
hashtable 是乱序