Articles in categories

Articles

Sort a Dictionary with Key or Value

Sort a Dictionary on the Key:

var mDict = new Dictionary<string, string>();
// fill the Dictionary with items here

// sort the Dictionary (IOrderedEnumerable) and convert back to Dictionary
var orderedDict = mDict.OrderBy(kv => kv.Key).ToDictionary(pair => pair.Key, pair => pair.Value);

And on the Value:

var mDict = new Dictionary<string, string>();
// fill the Dictionary with items here

// sort the Dictionary (IOrderedEnumerable) and convert back to Dictionary
var orderedDict = mDict.OrderBy(kv => kv.Value).ToDictionary(pair => pair.Key, pair => pair.Value);