Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The order of elements in Dictionary

People also ask

What order are dictionary keys in?

As of Python 3.6, for the CPython implementation of Python, dictionaries maintain insertion order by default.

What is the order of dictionary in Python?

Since dictionaries in Python 3.5 don't remember the order of their items, you don't know the order in the resulting ordered dictionary until the object is created. From this point on, the order is maintained. Since Python 3.6, functions retain the order of keyword arguments passed in a call.

How the elements are stored in dictionary Python?

In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by 'comma'. Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value.

Is dictionary ordered in C#?

OrderedDictionary Class represents a collection of key/value pairs that are accessible by the key or index. It is present in System.


The order of elements in a dictionary is non-deterministic. The notion of order simply is not defined for hashtables. So don't rely on enumerating in the same order as elements were added to the dictionary. That's not guaranteed.

Quote from the doc:

For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair<TKey, TValue> structure representing a value and its key. The order in which the items are returned is undefined.


You can always use SortedDictionary for that. Note that the dictionary is ordered by Key, by default, unless a comparer has been specified.

I'm skeptic regarding the use of OrderedDictionary for what you want since documentation says that:

The elements of an OrderedDictionary are not sorted by the key, unlike the elements of a SortedDictionary class.


If you want the elements ordered, use a SortedDictionary. An ordinary hastable/dictionary is ordered only in some sense of the storage layout.


The items will be returned in the order that they happen to be stored physically in the dictionary, which depends on the hash code and the order the items were added. Thus the order will seem random, and as implementations change, you should never depend on the order staying the same.

You can order the items when enumerating them:

foreach (KeyValuePair<string, string> kvp in _Dictionary.OrderBy(k => k.Value)) {
  ...
}

In framework 2.0 you would first have to put the items in a list in order to sort them:

List<KeyValuePair<string, string>> items = new List<KeyValuePair<string, string>>(_Dictionary);
items.Sort(delegate(KeyValuePair<string, string> x, KeyValuePair<string, string> y) { return x.Value.CompareTo(y.Value); });
foreach (KeyValuePair<string,string> kvp in items) {
  ...
}

For an OrderedDictionary:

 var _OrderedDictionary = new System.Collections.Specialized.OrderedDictionary();

_OrderedDictionary.Add("testKey1", "testValue1");
_OrderedDictionary.Add("testKey2", "testValue2");
_OrderedDictionary.Add("testKey3", "testValue3");

var k = _OrderedDictionary.Keys.GetEnumerator();
var v = _OrderedDictionary.Values.GetEnumerator();

while (k.MoveNext() && v.MoveNext()) {
    var key = k.Current; var value = v.Current;
}

Items are returned in the order that they are added.


Associative arrays (aka, hash tables) are unordered, which means that the elements can be ordered in any way imaginable.

HOWEVER, you could fetch the array keys (only the keys), order that alphabetically (via a sort function) and then work on that.

I cannot give you a C# sample because I don't know the language, but this should be enough for you to go on yourself.