开发者

unsort Hashtable

开发者 https://www.devze.com 2022-12-23 05:29 出处:网络
i am writing a program in C# i have a code like this Hashtable ht = new Hashtable(); ht.Add(\"1\", \"One\");

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


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>.


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.


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


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.


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.


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


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?


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"}
0

精彩评论

暂无评论...
验证码 换一张
取 消