var dir = new Dictionary<string, string>();
dir.Add("channelId", "1");
dir.Add("gameid", "1");
dir.Add("game_id", "1");
dir.Add("orderStatus", "1");
dir.Add("order_no", "1");
Console.WriteLine(String.Join("&", dir.OrderBy(a => a.Key, new AsciiCompareStrings()).Select(a => string.Format("{0}={1}", a.Key, a.Value))));
//channelId=1&game_id=1&gameid=1&orderStatus=1&order_no=1
Console.WriteLine(String.Join("&", dir.OrderBy(a
=> a.Key).Select(a =>
string.Format("{0}={1}", a.Key, a.Value))));
//channelId=1&game_id=1&gameid=1&order_no=1&orderStatus=1
/// <summary>
/// ascii码排序
/// </summary>
public class AsciiCompareStrings : IComparer<string>
{
/// <summary>
/// 以ascii码从小到大排序
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public int Compare(string s1, string s2)
{
return string.CompareOrdinal(s1, s2);
}
}
***************************************数组排序实现****************************************
//将集合key以ascii码从小到大排序
public static Dictionary<string, string> AsciiDictionary(Dictionary<string, string> sArray)
{
Dictionary<string, string> asciiDic = new Dictionary<string, string>();
string[] arrKeys = sArray.Keys.ToArray();
Array.Sort(arrKeys, string.CompareOrdinal);
foreach (var key in arrKeys)
{
string value = sArray[key];
asciiDic.Add(key, value);
}
return asciiDic;
}