Rya*_*yan 10 c# generics collections
以Value(而不是Key)的顺序对StringDictionary进行排序(或迭代)的"最佳"方法是什么?
例如Key - Value
会给
编辑 - 我想说"使用.NET 2.0功能".对不起,我不好......
dcp*_*dcp 14
使用LINQ:
var items = from k in d.Keys
orderby d[k] ascending
select k;
Run Code Online (Sandbox Code Playgroud)
如果您受限于C#2.0功能,请使用以下命令:
IDictionary<string, string> d = new Dictionary<string, string>();
d["1"] = "X label";
d["2"] = "A label";
d["3"] = "Other Label";
List<KeyValuePair<string, string>> myList = new List<KeyValuePair<string, string>>(d);
myList.Sort(
delegate(KeyValuePair<string, string> a,
KeyValuePair<string, string> b)
{
return a.Value.CompareTo(b.Value);
}
);
Run Code Online (Sandbox Code Playgroud)
注意:如果您使用的是StringDictionary而不是Dictionary,请查看Anthony的解决方案.