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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:47:45+00:00 2026-05-20T10:47:45+00:00

Say I have a Dictionary object which contains many instances of an object called

  • 0

Say I have a Dictionary object which contains many instances of an object called SomeObject and each SomeObject has properties named Property1 and Property2. Now say I have a method which can return a new Dictionary sorted by Property1. However, I’d like to generalize that method such that I can tell it which property to sort by.

For example, to do this, I know I could easily have two separate methods such as:

public Dictionary<string, SomeObject> SortByProp1(Dictionary<string, SomeObject> dict) { ... }
public Dictionary<string, SomeObject> SortByProp2(Dictionary<string, SomeObject> dict) { ... }

But, I’m wondering if it’s possible to combine these into one method in which I can give a parameter to identify which method to return? I don’t know of a way to generalize a property like that…

Note: I do realize I could always use something like an if-statement which could be based on a string of the property name, but that doesn’t seem like a very elegant, acceptable answer much better than just having two separate methods to begin with.

  • 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-20T10:47:46+00:00Added an answer on May 20, 2026 at 10:47 am

    The method you seek is built into the IEnumerable<T> interface, and is OrderBy, though it might not work out as smothly for a Dictionary as you might like.

    Dictionary<TKey, TValue> implements IEnumerable<KeyValuePair<TKey, TValue>>, so its OrderBy method takes a lambda expression which itself takes a KeyValuePair<TKey, TValue>, and returns a selector method that describes what you’re sorting by.

    For example, if you have Dictionary<int, Customer>, this will return to you an IEnumerable<KeyValuePair<int, Customer>> sorted by customer name:

    myDict.OrderBy(kvp => kvp.Value.Name);
    

    Then to get just the customers themselves in this sorted order, you’d say:

    IEnumerable<Customer> orderedByName = myDict.OrderBy(kvp => kvp.Value.Name).Select(kvp => kvp.Value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following City class. Each city object contains a dictionary which keys
Say I have an object called Person which has the property socialSecurityNumber , and
Lets say I have a flat list of objects, each of which has a
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
I have a generic dictonary which is templated in the following manner: Dictionary<object, IList<ISomeInterface>>
Let's say I have a Dictionary<string,object> like so: var foo = new Dictionary<string, object>();
Say I have a json object in variable somejson (a dictionary). I have a
Say you have a Dictionary < string,object > . It might contain decimals, strings
i have an array list of custom objects. each object contains a value i
So let's say I have a Clone object with these properties. Clone might have

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.