Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6091325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:18:51+00:00 2026-05-23T12:18:51+00:00

I have a collection of dictionary values (a List<> ) that I need to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:18:51+00:00Added an answer on May 23, 2026 at 12:18 pm
      private static IEnumerable<Dictionary<string, string>> Sort(IEnumerable<Dictionary<string,string>> data, string orderByString)
      {
         var orderBy =
            orderByString.Split(',').Select(
               s => s.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries))
               .Select (a => new {Field=a[0],Descending = "desc".Equals (a[1], StringComparison.InvariantCultureIgnoreCase)})
               .ToList ();
         if (orderBy.Count == 0)
            return data;
         // First one is OrderBy or OrderByDescending.
         IOrderedEnumerable<Dictionary<string, string>> ordered =
            orderBy[0].Descending ? data.OrderByDescending (d => d[orderBy[0].Field]) : data.OrderBy (d => d[orderBy[0].Field]);
         for (int i = 1; i < orderBy.Count; i++)
         {
            // Rest are ThenBy or ThenByDescending.
            var orderClause = orderBy[i];
            ordered =
               orderBy[i].Descending ? ordered.ThenByDescending(d => d[orderClause.Field]) : ordered.ThenBy(d => d[orderClause.Field]);
         }
         return ordered;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a Generic.Collection.List and Generic.Collection.Dictionary like List<String> MyList Dictionary<String, List<String>> MyDictionary I need
Suppose I have a Collection of some kind that itself contains Collections; e.g., Dictionary(Of
I have Collection List<Car> . How to compare each item from this collection with
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
I have a Dictionary<> collection that contains characters. The collection has items added and
I have a List of Dictionaries that have keys of type string and values
I have a Dictionary<int, string> which I want to take the Key collection into
I'm planning to have collection of items stored in a TCollection. Each item will
I have a collection that use as a cache. How can I limit the
I have a Dictionary<string, mystruct> instance, for which I am grabbing a list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.