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

  • Home
  • SEARCH
  • 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 9166491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:09:53+00:00 2026-06-17T15:09:53+00:00

I have a dictionary object with the type like the below. Dictionary<string, Dictionary<Roles, Dictionary<Period,

  • 0

I have a dictionary object with the type like the below.

Dictionary<string, Dictionary<Roles, Dictionary<Period, List<Product>>>>

The Roles (is an enum) that has “Preparers” & “Approvers” as their items. Similarly Period is another enum that has “Ahead” & “Past” items.

The List holds list of various products.

I have items in the following hierarchical structure in the dictionary.

"Sachin" --> Roles.Preparer --> Period.Past --> Products
"Sachin" --> Roles.Approver --> Period.Ahead --> Products
"Sachin" --> Roles.Approver --> Period.Ahead --> Products
"Sachin" --> Roles.Approver --> Period.Past --> Products

I will have to sort the dictionary in the following order.

"Sachin" --> Roles.Preparer --> Period.Ahead --> Products
"Sachin" --> Roles.Approver --> Period.Ahead --> Products
"Sachin" --> Roles.Preparer --> Period.Past --> Products
"Sachin" --> Roles.Approver --> Period.Past --> Products

This structure is required because I will have to iterate through every item and should add as part of the mail.

The actual code is like this.

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Basics
{
    class Product
    {
        public string Name { get; set; }
        public int Days { get; set; }

    }

    enum Period
    {
        Ahead,
        Past
    }

    enum Roles
    {
        Preparer,
        Approver
    }

    class Program
    {
        static void Main(string[] args)
        {
            DictionaryProcessing(new string[] { "sriram123@yahoo.com", "abhishek321@yahoo.com" });
        }

        private static void DictionaryProcessing(string[] emailIDs)
        {
            List<Product> products = new List<Product>();

            Product product1 = new Product() { Name = "Pencil", Days = 14 };
            Product product2 = new Product() { Name = "Eraser", Days = 2 };
            Product product3 = new Product() { Name = "Geometry Box", Days = 31 };

            products.Add(product1);
            products.Add(product2);
            products.Add(product3);

            Dictionary<string, Dictionary<Roles, Dictionary<Period, List<Product>>>> dict = new Dictionary<string, Dictionary<Roles, Dictionary<Period, List<Product>>>>();

            ///

            foreach (string emailID in emailIDs)
            {

                if (!dict.ContainsKey(emailID))
                    dict.Add(emailID, new Dictionary<Roles, Dictionary<Period, List<Product>>>());

                if (!dict[emailID].ContainsKey(Roles.Preparer))
                    dict[emailID].Add(Roles.Preparer, new Dictionary<Period, List<Product>>());

                if (!dict[emailID][Roles.Preparer].ContainsKey(Period.Ahead))
                    dict[emailID][Roles.Preparer].Add(Period.Ahead, new List<Product>());

                if (!dict[emailID][Roles.Preparer].ContainsKey(Period.Past))
                    dict[emailID][Roles.Preparer].Add(Period.Past, new List<Product>());

                ///

                if (!dict[emailID].ContainsKey(Roles.Approver))
                    dict[emailID].Add(Roles.Approver, new Dictionary<Period, List<Product>>());

                if (!dict[emailID][Roles.Approver].ContainsKey(Period.Ahead))
                    dict[emailID][Roles.Approver].Add(Period.Ahead, new List<Product>());

                if (!dict[emailID][Roles.Approver].ContainsKey(Period.Past))
                    dict[emailID][Roles.Approver].Add(Period.Past, new List<Product>());

                for (int i = 0; i < products.Count; i++)
                {
                    dict[emailID][Roles.Preparer][Period.Ahead].Add(products[i]);
                    dict[emailID][Roles.Preparer][Period.Past].Add(products[i]);
                    dict[emailID][Roles.Approver][Period.Past].Add(products[i]);
                    dict[emailID][Roles.Approver][Period.Ahead].Add(products[i]);
                }


            }
        }
    }
}
`

How can I sort it in this order? I am limited to use .NET 2.0 framework.

  • 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-06-17T15:09:54+00:00Added an answer on June 17, 2026 at 3:09 pm

    Dictionaries can not be sorted. They are not lists. Additionally:

    Your structure is bad – a Dictionary can not contain the same key more than once, so the samples you are providing are not even possible to create:

    "Sachin" --> Roles.Preparer --> Period.Past --> Products
    "Sachin" --> Roles.Approver --> Period.Ahead --> Products
    "Sachin" --> Roles.Approver --> Period.Ahead --> Products
    "Sachin" --> Roles.Approver --> Period.Past --> Products
    

    The “outer” dictionary can not contain the key “Sachin” more than once. The “inner dictionary” can not contain the role Approver more than once and also on the last level, Period.Past/Ahead can not be key more than once.

    Change your structure to List<T> instead, where T is an appropriate data structure, or, as other have already noted, change to typed datasets now to treat your structure like a table.

    EDIT
    I’m editing my answer now, just to make sure that we all understand what everyone is talking about.

    I’m saying, it is not possible for a dictionary to have the same key twice. So, according to this rule, your cases must be reduced to the following:

    "Sachin" --> Roles.Preparer --> Period.Past --> Products
    "Sachin" --> Roles.Approver --> Period.Ahead --> Products
    "Sachin" --> Roles.Approver --> Period.Past --> Products
    

    Now that we’re talking about something that applies to the rules, we can ask “how to sort?”. The answer: you can’t. A dictionary is by definition an unordered structure. You can, however, make sure to retrieve the values in a specific order. If you want to “sort” the products, so that the Past products always come before the Ahead products, just make sure to use the respective key first.

    EDIT 2

    Just realized this is based on a copy/paste error. The data you’re talking about should read:

    "Sachin" --> Roles.Preparer --> Period.Ahead --> Products
    "Sachin" --> Roles.Preparer --> Period.Past --> Products
    "Sachin" --> Roles.Approver --> Period.Past --> Products
    "Sachin" --> Roles.Approver --> Period.Ahead --> Products
    

    You said you’re using this code to add the items:

    for (int i = 0; i < products.Count; i++)
    {
        dict[emailID][Roles.Preparer][Period.Ahead].Add(products[i]);
        dict[emailID][Roles.Preparer][Period.Past].Add(products[i]);
        dict[emailID][Roles.Approver][Period.Past].Add(products[i]);
        dict[emailID][Roles.Approver][Period.Ahead].Add(products[i]);
    }
    

    Then you can use similar code to retrieve the items. Given an eMail-ID, the following would get you a list of items in the ordered by “preparer before approver” and “past before ahead”:

    List<Product> productsForEMailID = new List<Product>();
    
    productsForEMailID.AddRange(dict[emailID][Roles.Preparer][Period.Past]);
    productsForEMailID.AddRange(dict[emailID][Roles.Approver][Period.Past]);
    productsForEMailID.AddRange(dict[emailID][Roles.Preparer][Period.Ahead]);
    productsForEMailID.AddRange(dict[emailID][Roles.Approver][Period.Ahead]);
    

    The list of products is “sorted”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Dictionary(of String, Object) that I need to iterate through and
I have an application that takes a dictionary of files (file type, and list
I have a function which returns a List<Dictionary<string, object>> where object is a standard
I have a message definition file that looks like this struct1 { field=name type=string
I have a dictionary object Dictionary<string, Type> dict = new Dictionary<string, Type>(); I also
I have a Dictionary object where the Keys are of type string and the
I would like to have a dictionary object contains string keys and values, which
I have a Dictionary object that is formed using a double as its key.
I have a dictionary object which i would like to encrypt, then put it
I have a string data which I need to parse into a dictionary object.

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.