c# Hashtable 按键排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9280054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:41:40  来源:igfitidea点击:

c# Hashtable sorted by Keys

c#sortinghashtablejquery-ui-sortable

提问by John Ryann

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

回答by BrokenGlass

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

您可以SortedDictionary为此使用 a ,它将为您按键进行排序。在您的情况下, a SortedDictionary<string, int>会起作用:

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

回答by Matt Grande

Use a list instead of a hash (or convert your hash to a dictionary), and do this:

使用列表而不是散列(或将散列转换为字典),然后执行以下操作:

var dictionary = new Dictionary<string, int>();
var l = dictionary.Keys.ToList();
l.Sort();
foreach (var key in l)
{
    Console.WriteLine(dictionary[key]);
}

回答by dasblinkenlight

Because of the nature of hash tables, you cannot sort them on the key in place: they organize their keys in buckets based on their hash code, a value outside of hash table's control. However, you can read key-value pairs in whatever order that you like. Here is how you can do it using LINQ:

由于哈希表的性质,您不能根据键就地对它们进行排序:它们根据哈希码将键组织在桶中,这是哈希表无法控制的值。但是,您可以按您喜欢的任何顺序读取键值对。以下是使用 LINQ 的方法:

IDictionary<string, int> d = ...; // your hash table
var ordered = d.OrderBy(p => p.Key).ToList();
foreach (var p in ordered) {
    Console.WriteLine("Key: {0} Value: {1}", p.Key, p.Value);
}

回答by Diego

Using Linq is easy (using System.Linq):

使用 Linq 很简单 ( using System.Linq):

var sortedList = (from kv in MyDictionary select kv order by kv.Key).ToList<KeyValuePair<string, int>>();

That returns a list of KeyValuePair<string, int>.

这将返回一个列表KeyValuePair<string, int>

回答by Rafael Diego Nicoletti

The simplest way I found to "sort" hashtable is:

我发现对哈希表进行“排序”的最简单方法是:

var hash = new Hashtable();
var orderedKeys = hash.Keys.Cast<string>().OrderBy(c => c); // supposing you're using string keys
var allKvp = from x in orderedKeys select new{ key = x, value = hash[x] };

However, Im not ordering the original hashtable, only reading its values in an ordered way.

但是,我没有对原始哈希表进行排序,只是以有序的方式读取其值。

As in other replies, if you need to store your data is sorted way, the best is to use SortedDictionary

与其他回复一样,如果您需要以排序方式存储数据,最好使用 SortedDictionary

回答by Badar

I used a list to store keys of Hashtable and sorted it and then dislayed Hashtable using this sorted list. Here is my code:

我使用一个列表来存储 Hashtable 的键并对其进行排序,然后使用此排序列表显示 Hashtable。这是我的代码:

        List<string> lst = new List<string>();
        foreach (var key2 in ht.Keys)
        {
            lst.Add(key2.ToString());
        }
        lst.Sort();
        foreach (var item in lst)
        {
            Console.WriteLine(string.Format("{0},{1}", item, ht[item.ToString()]));
        }
        List<string> lst = new List<string>();
        foreach (var key2 in ht.Keys)
        {
            lst.Add(key2.ToString());
        }
        lst.Sort();
        foreach (var item in lst)
        {
            Console.WriteLine(string.Format("{0},{1}", item, ht[item.ToString()]));
        }