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 6379769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:10:50+00:00 2026-05-25T02:10:50+00:00

I have a struct Test, which I add as a value to a dictionary.

  • 0

I have a struct Test, which I add as a value to a dictionary.
What I want to do is sort the dictionary by the DateTime of the value.

    struct Test
    {
        DateTime dt;
        public string ID;
    }

    Dictionary<String, Test> dict = new Dictionary<String,Test>();
    Test t = new Test();

    t.dt = DateTime.Now;
    t.ID = "XUDF";
    dict.Add(t.ID, t);

    t.dt = DateTime.Now.AddDays(17);
    t.ID = "RFGT";
    dict.Add(t.ID, t);

    t.dt = DateTime.Now.AddDays(3);
    t.ID = "ADLV";
    dict.Add(t.ID, t);

    t.dt = DateTime.Now.AddHours(2);
    t.ID = "SKFU";
    dict.Add(t.ID, t);

I’m not sure what to do after this.
Also, is this the best way to go about it?
I’m using .net 3

What I am trying to do is have a list that I can access by the ID but also ordered by the datetime in t.dt.
I want to be able to select an object by its ID but also be able to iterate through and have it come out in order of datetime.

  • 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-25T02:10:50+00:00Added an answer on May 25, 2026 at 2:10 am

    What do you mean by “sort the dictionary”? A dictionary has no “order” to it per se. It’s a keyed collection, the underlying implementation of which is a hash table.

    I assume what you meant to say is, “I want to enumerate over the contents of the dictionary in order of the values.” That can be done thusly:

    public void List( Dictionary<string,DateTime> dict )
    {
      int i = 0 ;
      foreach( KeyValuePair<string,DateTime> entry in dict.OrderBy( x => x.Value ).ThenByDescending( x => x.Key ) )
      {
        Console.WriteLine( "{0}. Key={1}, Value={2}" , ++i , entry.Key , entry.Value ) ;
      }
      Console.WriteLine( "The dictionary contained a total of {0} entries." , i ) ;
    }
    

    If on the other hand, you actually want/need an ordered collection, you need to spec out what your actual requirements are.

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

Sidebar

Related Questions

For example, I have a struct which is something like this: struct Test {
I have the following .NET value types: [StructLayout(LayoutKind.Sequential)] public struct Date { public UInt16
I have a struct which has a unique key. I want to insert instances
I have a class which contains several structs: public class NavigationMenu { public struct
I have this struct: struct Map { public int Size; public Map ( int
I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
I have two c files, foo.c with the functionality and test_foo.c which test the
OK, I have this benchmark from SPLASH2 which I am using to test a
So I have something like this #define HASHSIZE 1010081 static struct nlist *hashtab[HASHSIZE]; Now
I've got a unit test I'm writing which seems to have some kind 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.