I have the next C# code:
Dictionary<string, int> d = new Dictionary<string, int>();
d.Add("a", 3);
d.Add("b", 1);
d.Add("c", 0);
d.Add("d", -1);
d.Add("e", -9);
When searching the key “c” I want to get the position of this key, i.e. 2. If I look for the key “e”, I want to get 4. If the element is not found the relative position could be -1.
Added:
Unless you have a better idea, I want to populate a matrix with certain values in a row number indicated by the relative position of a dictionary element found. The same applies for the column but using a different dictionary. An example:
n4 n2 n1 n3 n9 . . .
a 4/4
b 2 8
c
d 8/2
e 4/3
.
.
.
Where a,b,c,d,e,… are the keys of dictionay “d” and n4,n2,n3,n9 are the keys of a second dictionary.
How can I get this?
There’s no such thing as a “position” within a
Dictionary<,>– it’s an unordered collection.There are similar collections sorted by key –
SortedList<,>andSortedDictionary<,>. Note that those are ordered by key rather than insertion time though. It’s not clear which you want.