unsort Hashtable

删除回忆录丶 提交于 2019-12-25 04:47:37

问题


i am writing a program in C# i have a code like this

Hashtable ht = new Hashtable();
            ht.Add("1", "One");
            ht.Add("2", "Two");
            ht.Add("3", "Three");
            ht.Add("4", "Four");

but Compiler sort it i wanna know how can i prevent sorting hashtable please help me


回答1:


A HashTable doesn't do sorting as such. It does rearrange the items based on their hash code, so the original order isn't preserved.

If you want to preserve the original order, or want to specify a sort order, you can't use a HashTable only.

To specify a different sort order, you can use a SortedDictionary<T>. To preserve the original order, you can add the items both to a Dictionary<T> and a List<T>.




回答2:


What do you mean by the compiler sorting it? There is definitely NO sorting done. How are you looping over the items?

I think that order of items when you loop over the keys isn't guaranteed ->

foreach(object key in hashtable.Keys){
...
}

but from your question I think you'd like to retrieve the items in the exact same order as you have inserted them - maybe the best solution would be to keep parallel List of your keys; and retrieve keys for looping over hashtable from there.




回答3:


Use a Dictionary<int, string> or Dictionary <string, string> instead.




回答4:


When doing:

        Hashtable ht = new Hashtable();
        ht.Add("1", "One");
        ht.Add("2", "Two");
        ht.Add("3", "Three");
        ht.Add("4", "Four");

        foreach (var k in ht.Keys)
        {
            Console.WriteLine(k);
        }

I see no sorting of any kind taking place.




回答5:


Hashtables do not preserve the order of inserted items. They are stored in a data structure that has no concept of a "correct" order. You should assume that items in a hashtable will be stored in a random order.

You should use a List or other structure instead of a Hashtable if the order of the items is important.




回答6:


Try this:

    Hashtable ht = new Hashtable();
    ht.Add("1", "One");
    ht.Add("2", "Two");
    ht.Add("3", "Three");
    ht.Add("4", "Four");

    foreach (var k in ht.Keys.sort)
    {
        Console.WriteLine(k);
    }

Notice the sort after ht.Keys




回答7:


To get the Values in your hashtable ordered by key (wich is what it looks like you want) use this:

public List<string> GetOrderedValues(HashTable ht)
{
    // Get a sorted list of keys
    List<string> keys = new List<string>(ht.Keys.Cast<string>());
    keys.Sort();

    // Get values sorted by key
    List<string> values = new List<string>();
    foreach (string key in keys)
        values.Add(ht[key]);

    // Return Sorted Values
    return values;
}

You can also separete the first part and then you can get both a list of sorted keys and a list of sorted values.

Another option is return a List of KeyValuePair wichh will be sorted by entry

Then again, why not just remove all the ashle and replace your hashtable with a List of KeyValuePair in the first place?




回答8:


I found this blog, http://mctexpert.blogspot.com/2014/12/keeping-hash-table-in-order.html

To be specific, using [Ordered] would help with keeping the original sort:

$Hash = [Ordered]@{"Apple"="Red";

"Orange"="Orange";

"Banana"="Yellow";

"Pear"="Green";

"Blueberry"="Blue";

"Plum"="Purple"}


来源:https://stackoverflow.com/questions/2453624/unsort-hashtable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!