dictionary排序方法-创新互联
假设有这样的乱序dictionary
分享题目:dictionary排序方法-创新互联
浏览地址:http://scpingwu.com/article/gcgoi.html

Dictionary d_Last10Mins_RQIM = new Dictionary();
d_Last10Mins_RQIM.Add("1", 10.0);
d_Last10Mins_RQIM.Add("2", 20.0);
d_Last10Mins_RQIM.Add("3", 30.0);
d_Last10Mins_RQIM.Add("4", 90.0);
d_Last10Mins_RQIM.Add("5", 11.0);
d_Last10Mins_RQIM.Add("6", 23.0);
如果想按照 value 排序 , 可以
var Last10MinsSortedDict = (from entry in d_Last10Mins_RQIM orderby entry.Value ascending select entry)
.ToDictionary(pair=> pair.Key, pair => pair.Value);
Last10MinsSortedDict 就是排序后的dictionary , 可以像这样引用
var first = Last10MinsSortedDict.First();
string firstVendor = first.Key;
var last = Last10MinsSortedDict.Last();
string lastVendor = last.Key;
或者foreach
foreach (var pair in Last10MinsSortedDict)
{
}
要逆序排就把 ascending 改为 descending
分享题目:dictionary排序方法-创新互联
浏览地址:http://scpingwu.com/article/gcgoi.html