I have a collection of dictionary values (a List<>) that I need to sort. Basically each dictionary is a ‘row’ and the collection is a page of rows. A simple example;
var data = new List<Dictionary<string,string>>();
data.Add(new Dictionary<string,string>() {
{ "Firstname", "Bob"},
{ "Lastname", "Banana"}
});
data.Add(new Dictionary<string, string>() {
{ "Firstname", "Amy"},
{ "Lastname", "Apple"}
});
data.Add(new Dictionary<string, string>() {
{ "Firstname", "Charlie"},
{ "Lastname", "Coconut"}
});
data.Add(new Dictionary<string, string>() {
{ "Firstname", "Andy"},
{ "Lastname", "Apple"}
});
The sort string generated is “SQL” like, example
Lastname asc, Firstname desc
I have tried .OrderBy() on the data object but that doesn’t seem to work right against the KeyValuePairs.
Any idea how I could get the data list to be sorted to be in this order, using the dynamic sort statement:
Apple, Andy
Apple, Amy
Banana, Bob
Coconut, Charlie
Using .NET 4.0 if some fancy LINQ will work. Thanks for any suggestions.
1 Answer