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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:40:01+00:00 2026-05-14T02:40:01+00:00

I have a Dictionary<> which I want to sort based on value so I’ve

  • 0

I have a Dictionary<> which I want to sort based on value so I’ve done this by putting the dictionary into a List<> then using the .Sort method.

I’ve then added this back into a Dictionary<>. Is it possible to lookup the new index/order by using the Dictionary key??

Dictionary<int, MyObject> toCompare = new Dictionary<int, MyObject>();

toCompare.Add(0, new MyObject());
toCompare.Add(1, new MyObject());
toCompare.Add(2, new MyObject());

Dictionary<int, MyObject> items = new Dictionary<int, MyObject>();
List<KeyValuePair<int, MyObject>> values = new List<KeyValuePair<int, MyObject>>   (toCompare);

// Sort.
values.Sort(new MyComparer());

// Convert back into a dictionary.
foreach(KeyValuePair<int, PropertyAppraisal> item in values)
{
      // Add to collection.
  items.Add(item.Key, item.Value);
}

// THIS IS THE PART I CAN'T DO...
int sortedIndex = items.GetItemIndexByKey(0);
  • 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-14T02:40:01+00:00Added an answer on May 14, 2026 at 2:40 am

    Keep your data in the Dictionary<TKey,TValue>, but use a List<TKey> to sort the keys, then iterate as such:

    IDictionary<int, MyObject> dict = new Dictionary<int, MyObject>();
    // ... Populate dict with data.
    
    IList<int> keyList = new List<int>();
    keyList.AddRange(dict.Keys);
    
    // Sort keyList based on key's value.
    // MyObject must implement IComparable<MyObject>.
    keyList.Sort(delegate(int x, int y) {
       return dict[x].CompareTo(dict[y]);
    });
    
    foreach (int key in keyList) {
       MyObject value = dict[key];
    }
    

    This way, your list is merely a sorted index and does not affect your storage algorithm.

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

Sidebar

Related Questions

I have a Dictionary<int, string> which I want to take the Key collection into
I have a list of List<KeyvaluePair<DateTime, int>> , which I want to merge into
I have a dictionary of arrays which I want to filter based on an
I have a dictionary which has value as a list. dictionary = { 'C1'
I have a list of fields which a user can sort using jquery. I
While Using Python Dictionary DataStructure (which contains key-value pair) if i want to retrieve
I have an interface called Dictionary which has a method insert() . This interface
I have a bunch of named value parameters in a Dictionary<string, object> , which
I often have to sort a dictionary (consisting of keys & values) by value.
I have a dictionary dict2 which I want to iter through and remove all

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.