Icemanind
Icemanind

Reputation: 48686

Adding a dictionary element at a specific place

Consider the following code:

var myDict = new Dictionary<string, int>();

myDict.Add("Key1", 1);
myDict.Add("Key2", 2);
myDict.Add("Key4", 4);
myDict.Add("Key5", 5);

foreach (KeyValuePair<string, int> pair in myDict)
{
    Console.Write(pair.Key + @" --> ");
    Console.WriteLine(pair.Value);
}

myDict.Add("Key3", 3);
foreach (KeyValuePair<string, int> pair in myDict)
{
    Console.Write(pair.Key + @" --> ");
    Console.WriteLine(pair.Value);
}

What I want to do is insert "Key3" in between "Key2" and "Key4". I am using this as an example for simplicity's sake. I know I could use a SortedDictionary and I could get this example to work. What I need to be able to do specifically though is, whenever I insert a new element into the dictionary, I ALWAYS want it to insert it after the 2nd element and before the 3rd element. How can I accomplish this?

Upvotes: 4

Views: 9307

Answers (6)

Sam Axe
Sam Axe

Reputation: 33738

Dictionaries are meant to have their values accessed via their keys.. not via indices. So you should think about using an array or List instead.

Upvotes: 1

JaredPar
JaredPar

Reputation: 754545

This is not possible with a standard Dictionary<TKey, TValue> type. It is inherently an unordered collection. Any attempt at guaranteeing ordering will not work.

With a SortedDictionaryTKey, TValue> this could work if you make the comparer aware of an elements place in the Dictionary. However this is almost certainly the wrong approach. It really sounds like you want a List<T> here..

Upvotes: 2

Jon
Jon

Reputation: 437336

You can use an OrderedDictionary for this. OrderedDictionary.Insert allows you to specify the index at which the key will be inserted.

Upvotes: 12

Polynomial
Polynomial

Reputation: 28316

The Dictionary<TKey,TVal> class. has no concept of sorted keys. You want a SortedDictionary, which will sort your dictionary by the key.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499770

There's no concept of "between" in a Dictionary<,>. It's an unordered mapping. Iterating over it will give unpredictable results which may change between framework versions. If you want to preserve some sort of ordering, you could have a separate List<T> or LinkedList<T> representing the notional key order. You'd have to keep them up to date with respect to each other.

Upvotes: 3

thecoop
thecoop

Reputation: 46098

This is not what a dictionary is for. A dictionary is unsorted, by definition. There is no such thing as the 2nd or 4th element, as they are only accessed by key.

The items are enumerated in the order you add them simply because of the internal implementation of Dictionary used by the particular CLR version you happen to be running (see here for a post on how Dictionary is actually implemented to understand why it does this)

Use a SortedDictionary or SortedList instead, or, if you want to insert into particular indexes, roll your own collection combining a Dictionary (for O(1) key lookup) and a List (to maintain the key order and insert at a particular index). OrderedDictionary might do what you are looking for, but it's a non-generic collection.

Upvotes: 1

Related Questions