C parameters are sorted from small to large according to ASCII code (lexicographic order)

  • 2021-12-12 09:33:09
  • OfStack

When paying for the third party, the third party will require the parameters to be sorted from small to large according to ASCII code. As follows:


public static void requestPay()
 {  Dictionary<string, string> dics = new Dictionary<string, string>();
  dics.Add("amount", amount);
  dics.Add("callback_url", callback_url);
  dics.Add("goodsname", goodsname);
  dics.Add("merchno", merchno);
  dics.Add("notify_url", notify_url);
  dics.Add("ordno", ordno);
  dics.Add("organno", organno);
  dics.Add("version", version);
  dics.Add("paytype", paytype);
  getParamSrc(dics);
}

public static String getParamSrc(Dictionary<string, string> paramsMap)
{
  var vDic = (from objDic in paramsMap orderby objDic.Key ascending select objDic);
  StringBuilder str = new StringBuilder();
  foreach (KeyValuePair<string, string> kv in vDic)
  {
   string pkey = kv.Key;
   string pvalue = kv.Value;
   str.Append(pkey + "=" + pvalue + "&");
  }
  String result = str.ToString().Substring(0, str.ToString().Length - 1);
  return result;
}

Related articles: