I am refering to this (Linq OrderBy against specific values) question:
I want to sort a Dictionary with very few values suiting my needs. There will only be 6 entries with keys that should be sorted after my custom logic. I am thinking about one data-dictionary and one pref-dictionary:
Dictionary<string, int> data = new Dictionary<string,int>() {
{"Test16", 10},
{"What61", 8},
{"Blub11", 14},
{"Int64", 13}
};
Dictionary<string, int> preferences = new Dictionary<string, int>() {
{"Blub11", 1},
{"What61", 2},
{"Int64", 3},
{"Test16", 4}
};
// desired Output:
// data =
// {"Blub11", 14},
// {"What61", 8},
// {"Int64", 13},
// {"Test16", 10}
string key;
data = data.OrderBy(
item => preferences.TryGetValue(item, out key) ? data[key] : item
);
I can't get this to work and must admit that I am not familiar with lambda expressions and linq so a simple solution would be appreciated. Thank you so far.