0

When I remove Dictionary key add then add new key to dictionary new value is not added to last location. Instead of its adding in the key where it is removed.

 Dictionary<int, string> dic = new Dictionary<int, string>();
 dic.Add(1, "a");
 dic.Add(2, "b");
 dic.Add(3, "c");

 dic.Remove(2);

 dic.Add(4, "d");

I want output as

1 "a"
3 "c"
4 "d"

Not as

1 "a"
4 "d"
3 "c"
Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
anishMarokey
  • 11,279
  • 2
  • 34
  • 47
  • 5
    If you care about ordering, you're using the wrong type, a Dictionary is not the answer here. – Aaron McIver Nov 15 '11 at 15:37
  • not ordering. it should be in any order but i want to add it in last not in the key removed location. – anishMarokey Nov 15 '11 at 15:44
  • If you are concerned with _location_ you are concerned with _ordering_. – Aaron McIver Nov 15 '11 at 15:53
  • possible duplicate of [Generic Key/Value pair collection in that preserves insertion order?](http://stackoverflow.com/questions/1396718/generic-key-value-pair-collection-in-that-preserves-insertion-order) – nawfal Oct 31 '13 at 07:38

5 Answers5

4

Dictionarys are not guaranteed to be in the order of insertion. SortedDictionarys are:

SortedDictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "a");
dic.Add(2, "b");
dic.Add(3, "c");

dic.Remove(2);

dic.Add(4, "d");

Produces:

1 "a"
3 "c"
4 "d"
CassOnMars
  • 6,153
  • 2
  • 32
  • 47
  • SortedDictionary is in .NET 2.0: http://msdn.microsoft.com/en-us/library/f7fta44c%28v=VS.80%29.aspx – CassOnMars Nov 15 '11 at 15:38
  • if key is not in sorted order then? – anishMarokey Nov 15 '11 at 15:41
  • 1
    If the key is not in a sorted order, then what you are trying to do is a `List>`. This is the only way to directly preserve order of insertion and not sort by anything else, unless you want to create a custom type. – CassOnMars Nov 15 '11 at 15:43
3

Dictionaries do not necessarily preserve ordering. Your either looking for the SortedDictionary class, or should just sort the key value pairs when printing your results.

wsanville
  • 37,158
  • 8
  • 76
  • 101
2

From here:

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

As stated by others, you need a data structure that preserves order such as a SortedDictionary (preserves order by value) or a SortedList (preserves insertion order). (see 4.0 versions here and here and 2.0 versions here and here)

N_A
  • 19,799
  • 4
  • 52
  • 98
2

If you manage the sorting by yourself, you can use a List<KeyValuePair<int, string>>. In a list you use the Insert-method to insert the item at the position you want. The order in the list remains.

Fischermaen
  • 12,238
  • 2
  • 39
  • 56
0

Dictionary is unsorted. (Random access map)

You can use SortedDictionary to keep it sorted by key.

Or if you want to sort by value (not key) check this out:

How do you sort a dictionary by value?

Community
  • 1
  • 1
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185