C#哈希表进行排序由钥匙 [英] c# Hashtable sorted by Keys

查看:118
本文介绍了C#哈希表进行排序由钥匙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在字母键和值在数字哈希表。
如何根据键哈希表进行排序?



  ExchangeA,200 
ExchangeV,100
ExchangeC,200

是这样

  ExchangeA,200 
ExchangeC,200
ExchangeV,100


解决方案

您可以使用 SortedDictionary 这将由键为你做了排序。在你的情况下, SortedDictionary<字符串,整数> 将工作:

  SortedDictionary<字符串,整数>字典=新SortedDictionary<字符串,整数>(); 
dict.Add(「联交所C,200);
dict.Add(「联交所A,200);
dict.Add(交换V,100);

的foreach(在字典VAR KVP)
{
Console.WriteLine(键= ​​{0},值= {1},kvp.Key,kvp.Value) ;
}



输出:

 键=交换A,值= 200 
键=交换C,值= 200
键=交换V,值= 100


I have a hashtable with keys in alphabetic and values in numeric. how to sort the hashtable based on keys?

ExchangeA, 200
ExchangeV, 100
ExchangeC, 200

to be like this

ExchangeA, 200
ExchangeC, 200
ExchangeV, 100

解决方案

You can use a SortedDictionary for this which will do the sorting by key for you. In your case a SortedDictionary<string, int> would work:

SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
dict.Add("Exchange C", 200);
dict.Add("Exchange A", 200);
dict.Add("Exchange V", 100);

foreach (var kvp in dict)
{
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}

Output:

Key = Exchange A, Value = 200
Key = Exchange C, Value = 200
Key = Exchange V, Value = 100

这篇关于C#哈希表进行排序由钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆