C# 参数按照ASCII码从小到大排序(字典序)
在对接第三方支付的时候,第三方会要求参数按照ASCII码从小到大排序。如下:
1 public static void requestPay() 2 { 3 Dictionary<string, string> dics = new Dictionary<string, string>(); 4 dics.Add("amount", amount); 5 dics.Add("callback_url", callback_url); 6 dics.Add("goodsname", goodsname); 7 dics.Add("merchno", merchno); 8 dics.Add("notify_url", notify_url); 9 dics.Add("ordno", ordno); 10 dics.Add("organno", organno); 11 dics.Add("version", version); 12 dics.Add("paytype", paytype); 13 14 getParamSrc(dics); 15 } 16 17 public static String getParamSrc(Dictionary<string, string> paramsMap) 18 { 19 var vDic = (from objDic in paramsMap orderby objDic.Key ascending select objDic); 20 StringBuilder str = new StringBuilder(); 21 foreach (KeyValuePair<string, string> kv in vDic) 22 { 23 string pkey = kv.Key; 24 string pvalue = kv.Value; 25 str.Append(pkey + "=" + pvalue + "&"); 26 } 27 28 String result = str.ToString().Substring(0, str.ToString().Length - 1); 29 return result; 30 }