Dictionary<string, string> 排序

.net framework 2.0 版

Dictionary<string, string> collection = new Dictionary<string, string> ();

collection.Add("key3","value3");

collection.Add("key1","value1");

collection.Add("key4","value4");

collection.Add("key2","value2");

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

list.Sort(delegate(KeyValuePair<string, string> pair1, KeyValuePair<string, string> pair2)
{
  return pair1.Key.CompareTo(pair2.Key);
});

StringBuilder sb = new StringBuilder();

foreach (KeyValuePair<string, string> pair in list)
{
  sb.Append(pair.Key);
  sb.Append("=");
  sb.Append(pair.Value);
  sb.Append("&");
}

string str = sb.ToString().TrimEnd('&');

//str 值key1=value1&key2=value2&key3=value3&key4=value4

 

posted @ 2014-11-11 10:04  随学·笔记  阅读(3109)  评论(0编辑  收藏  举报