是否有可能进行排序一个Hashtable? [英] Is it possible to sort a HashTable?
本文介绍了是否有可能进行排序一个Hashtable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个返回的HashTable
属性。我想没有我的重构属性排序。的请注意:的:我不希望返回另一种类型。
码:
///<总结>
///所有内容的容器。
///< /总结>
公共Hashtable的集装箱
{
得到
{
Hashtable的TBL =新的Hashtable();
的foreach(在Form.Controls控制CTRL)
{
如果(CTRL是占位符)
{
tbl.Add(ctrl.ID,CTRL);
}
//同时检查是否存在与内容占位符的用户控件。
,否则如果(CTRL为用户控件)
{
的foreach(在ctrl.Controls控制CTRL2)
{
如果(CTRL2为占位符)
$ { b $ b tbl.Add(ctrl2.ID,CTRL2);
}
}
}
}
返回TBL;
}
}
解决方案
另外选项是构建哈希表为你已经在做,然后简单地构建从按键有序集合。您可以通过排序按键迭代,从获取需要的哈希表中的相应值。
I have a property that returns a HashTable
. I would like to sort it without refactoring my property. Please note: I do not want to return another type.
Code:
/// <summary>
/// All content containers.
/// </summary>
public Hashtable Containers
{
get
{
Hashtable tbl = new Hashtable();
foreach (Control ctrl in Form.Controls)
{
if (ctrl is PlaceHolder)
{
tbl.Add(ctrl.ID, ctrl);
}
// Also check for user controls with content placeholders.
else if (ctrl is UserControl)
{
foreach (Control ctrl2 in ctrl.Controls)
{
if (ctrl2 is PlaceHolder)
{
tbl.Add(ctrl2.ID, ctrl2);
}
}
}
}
return tbl;
}
}
解决方案
Another option is to construct the hash table as you're already doing, and then simply construct a sorted set from the keys. You can iterate through that sorted key set, fetching the corresponding value from the hash table as needed.
这篇关于是否有可能进行排序一个Hashtable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文