首页 > 其他 > 详细

dictionary 排序方法

时间:2014-02-11 22:18:20      阅读:363      评论:0      收藏:0      [点我收藏+]

假设有这样的乱序dictionary

bubuko.com,布布扣
            Dictionary<string, double> d_Last10Mins_RQIM = new Dictionary<string, double>();
            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);
bubuko.com,布布扣

如果想按照 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://www.cnblogs.com/lthxk-yl/p/3544507.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!