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

时间: 2024-08-12 18:55:53

Dictionary<string, string> 排序的相关文章

转:Dictionary&lt;int,string&gt;怎么获取它的值的集合?急!急!急!

怎么获取Dictionary<int, string>的值?我知道这个是键值对的,我知道可以根据key得到value,但关键是现在连key也不知道啊就是想让这个显示在listbox中,应该怎么显示啊,在线等!!!! 1 //Ada 2 Dictionary<int, string> dic = new Dictionary<int, string>(); 3 dic.Add(1, "A"); 4 dic.Add(2, "B");

List&lt;HashMap&lt;String,String&gt;&gt; list, 根据hashmap中的某个键的值排序

//可以使用Collections.sort(List list, Comparator c)来实现 这里举例hashmap中存的一个时间的键值,按照时间的值来排序 //先写个类实现Comparator,并重写compare(Object o1, Object o2)方法,在方法中自定义比较逻辑 public class MyComparator implements Comparator { @Override     public int compare(Object o1, Object 

map&lt;String ,String&gt; 自动排序 升序

//TreeMap自动排序 Map<String, String> params = new TreeMap<String, String>( new Comparator<String>() { public int compare(String obj1, String obj2) { // 升序排序 return obj1.compareTo(obj2); } });

c# 关于字典dictionary 按时间排序

上文中说到sortedlist 排序是键排序,不符合项目要求问题,接着使用字典dictionary 对value 为时间按照升序排序,问题解决.中间涉及到linq的使用.还有其他的写法,但发现下边的写法最直观也容易理解.var dicSort = from objDic in dic orderby objDic.Value descending select objDic; 这种写法最简单,也容易理解dicsort 目标字典,objdic对象应该是映射的无需定义的对象,dic 是你自己定义的字

C# Dictionary根据Key排序

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Dictionary<int, string> test = new Dictionary<int, string> { }; test.Add(0,

(后缀数组)HDU 6194-string string string

string string string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 774    Accepted Submission(s): 225 Problem Description Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Un

Babelfish (map 用法。&lt;string, string&gt;

Babelfish - You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them. Input Input consists of up to 100,000 dictionary ent

KeyValuePair&lt;string, string&gt;

static int _timeout = 100000; #region CUP Method /// <summary> /// 请求与响应的超时时间 /// </summary> static public int Timeout { get { return _timeout; } set { _timeout = value; } } /// <summary> /// 执行HTTP POST请求. /// </summary> /// <p

从为什么String=String谈到StringBuilder和StringBuffer

前言 有这么一段代码 1 public class TestMain 2 { 3 public static void main(String[] args) 4 { 5 String str0 = "123"; 6 String str1 = "123"; 7 System.out.println(str0 == str1); 8 } 9 } 运行结果是什么?答案当然是true.对,答案的确是true,但是这是为什么呢?很多人第一反应肯定是两个“123”的Stri