2

I have a Dictionary<string, Func<string, string>>. I use the keys to provide the items of a drop-down which allows the user to select which algorithm to run. Then I can conveniently feed the selected value back into the dictionary to get the relevant function.

// Initialization
my_dict["Function #1"] = func1;
my_dict["Function #2"] = func2;

...

// Executing
result = my_dict[user_choice](input);

Now, when populating the drop down (a System.Windows.Forms.ComboBox) I loop through the keys of this with foreach. MSDN warns that System.Collections.Generic.Dictionary is not guaranteed to have a stable order. But in practice I know that the order is pretty stable, so it works for now. I do want the functions to be ordered nicely for the user.

However, I want to do this correctly - ie. by using a data structure that guarantees order stability. Unfortunately OrderedDictionary is not helpful, since it forces the values to be objects; I'd like to keep them function delegates. At the same time, I don't want to add too much needless complexity (for instance, I could store a copy of the keys as a List<string> to use for order, but that seems like a poor solution).

What can I do?

Note: Looks like some people assumed that I want my items to be in alphabetical order. I don't. I want them to stay in the same order that I added them in. That is, if key Function #2 is added before Function #1, it will come before it in the order.

Superbest
  • 25,318
  • 14
  • 62
  • 134
  • Can you please clarify why basic sorted `List>>` does not work? (assuming your "drop-downs" support selected value/index along with selected text)... – Alexei Levenkov Jun 18 '15 at 05:26
  • In case you plan to use SortedDictionary, then till the point your key is string its fine, else you need a IEqualityComparer for TKey, check this link - http://stackoverflow.com/questions/2705607/sorting-a-dictionary-in-place-with-respect-to-keys – Mrinal Kamboj Jun 18 '15 at 06:33
  • perhaps you need to implement `IDictionary` on your own. – Daniel A. White Jun 19 '15 at 11:17
  • @AlexeiLevenkov you should post your solution as an answer because the other two answers are not answers to this question. – user829755 Jun 06 '19 at 08:28
  • Use non-generic OrderedDictionary or implement it yourself as in this older question: https://stackoverflow.com/questions/2629027/no-generic-implementation-of-ordereddictionary/64214691?noredirect=1#comment113553608_64214691 – mireazma Oct 06 '20 at 09:21

2 Answers2

6

Instead of using the OrderedDictionary, you should use the SortedDictionary<TKey, TValue>.

This one is sorted by the key. So for your example by your Strings. This should suffice.

Update:

If you are not using any native comparable type you need to implement the IEqualityComparer<T> interface for your key-class.

ckruczek
  • 2,361
  • 2
  • 20
  • 23
-3

You can also use lambda expression to sort dictionary by Key or by Value like this.

 Dictionary<int, string> myDic = new Dictionary<int, string>();
                myDic.Add(1, "a");
                myDic.Add(2, "b");
                myDic.Add(3, "c");
                myDic.Add(4, "d");
                myDic.Add(5, "e");
                myDic.OrderBy(cond => cond.Key); //sort by key
                myDic.OrderBy(cond => cond.Value); //sort by value
Dev D
  • 225
  • 1
  • 13