Dictionary、List<KeyValuePair<i>>的组合使用

菜菜遇到一个有意思的问题:Dictionary字典没有排序功能,遇到需要特殊排序的还要倒腾成List<KeyValuePair<>>,再在sort()函数中写委托排序;而List<KeyValuePair<>>只能支持读取操作,即只能get读取操作,无法set改变值;所以遇到既需要遍历改变某些值,然后再按照所需排序这样的操作,往往需要Dictionary和List<KeyValuePair<>>一起操作完成,如下所示:


 private List<KeyValuePair<int,string>> getdic(List<KeyValuePair<int, string>> list3, List<string> list5,List<string> list6)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();


            List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();


            foreach (KeyValuePair<int, string> var in list3)
            {
                dic.Add(var.Value,2);


            }


            //处理既往病史
            foreach (string key in list5)
            {
                switch (key)
                {
                    case "高血压":
                    case "糖尿病":
                    case "高血脂":
                    case "肿瘤":
                    case "脑卒中":
                        {
                            if (dic.ContainsKey(key))
                            {
                               dic[key]=dic[key]+1;  微笑字典中值可以改变,但是如果是KeyValuePair,其key与value属性都是get类型,所以只能读取,但是不能设定值
                            }
                            else
                            {
                                dic.Add(key, 3);
                            }


                        }
                        break;


                    case "冠心病/心绞痛":
                        {


                            if (dic.ContainsKey("心"))
                            {
                                dic["心"] = dic["心"] + 1;
                            }
                            else
                            {
                                dic.Add("心", 3);
                            }




                        }
                        break;
                    case "慢性支气管炎":
                        {
                            if (dic.ContainsKey("肺"))
                            {
                                dic["肺"] = dic["肺"] + 1;
                            }
                            else
                            {
                                dic.Add("肺", 3);
                            }
                        }
                        break;
                    case "肝炎":
                        {
                            if (dic.ContainsKey("肝"))
                            {
                                dic["肝"] = dic["肝"] + 1;
                            }
                            else
                            {
                                dic.Add("肝", 3);
                            }
                        }
                        break;


                    case "慢性胃炎/胃溃疡":
                        {
                            if (dic.ContainsKey("胃"))
                            {
                                dic["胃"] = dic["胃"] + 1;
                            }
                            else
                            {
                                dic.Add("胃", 3);
                            }
                        }
                        break;
                    case "慢性肠炎":
                        {
                            if (dic.ContainsKey("小肠"))
                            {
                                dic["小肠"] = dic["小肠"] + 1;
                            }
                            else
                            {
                                dic.Add("小肠", 3);
                            }
                        }
                        break;
                    default:
                        break;
                }
            }


            //处理疾病家族史
            foreach (string key in list6)
            {
                switch (key)
                {
                    case "高血压":
                    case "中风/脑溢血":
                    case "糖尿病":
                    case "肿瘤":
                        {
                            if (dic.ContainsKey(key))
                            {
                                dic[key] = dic[key] + 1;
                            }
                            else
                            {
                                dic.Add(key, 3);
                            }
                        }
                        break;
                    case "冠心病/心绞痛":
                        {
                            if (dic.ContainsKey("心"))
                            {
                                dic["心"] = dic["心"] + 1;
                            }
                            else
                            {
                                dic.Add("心", 3);
                            }


                        }
                        break;


                    default:
                        break;
                }
            }


            //return dic;
            foreach (string key in dic.Keys)
            {
                list.Add(new KeyValuePair<int, string>(dic[key], key));微笑把字典转化成List<KeyValuePair<>>在sort()函数中写委托按照自己想要的方式排序
            }


            list.Sort(delegate(KeyValuePair<int, string> s1, KeyValuePair<int, string> s2)
            {
                return s2.Key.CompareTo(s1.Key);
            
            });

            return list;


        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值