-1

I have a Dictionary<string, Dictionary<string, object>> that needs to be sorted depending on a certain value of the child dictionary.

e.g

{
  "James": {
    "Age":  21,
    "Surname": "Doe",
    "Temp": 80.6
  },
  "Tobi": {
    "Age":  26,
    "Surname": "Doe",
    "Temp": 60.5
  }
}

The dictionary with key James and Tobi needs to be sorted depending on i.e. the temp value. The string "Temp" would be given to the method to indicate the key to be sorted on.

The object values type would differ depending on the context but the type would be known to the method allowing to cats the object to the correct type for comparison.

Creating a specific object would not be possible for the solution as dictionary<string, object> represents the object properties and would differ with each implementation of the method.

All child dictionaries would have the same "properties" so the child dictionary not having the value would not be a problem.

so in plain description, a method taking in:

{
  "James": {
    "Age":  21,
    "Surname": "Doe",
    "Temp": 80.6
  },
  "Tobi": {
    "Age":  26,
    "Surname": "Doe",
    "Temp": 60.5
  },
  "Jessica": {
    "Age":  18,
    "Surname": "Samms",
    "Temp": 50.67
  }
}

and

string "Temp"

and posibily Temp type of

string "double"

that returns

IEnumerable<KeyValuePair<string,Dictionary<string,object>>>

{
  "Jessica": {
    "Age":  18,
    "Surname": "Samms",
    "Temp": 50.67
  },
  "Tobi": {
    "Age":  26,
    "Surname": "Doe",
    "Temp": 60.5
  },
  "James": {
    "Age":  21,
    "Surname": "Doe",
    "Temp": 80.6
  }
}
5
  • Why are you using a dictionary? Have you considered to transform the items to actual objects? Commented Oct 2, 2020 at 9:19
  • I don't think this is a Dictionary<string, Dictionary<string, object>> but rather a Dictionary<string, T>. Take a look at my sample: dotnetfiddle.net/J8DgS5 Commented Oct 2, 2020 at 9:19
  • You can't sort a dictionary, it's a hashset Commented Oct 2, 2020 at 9:20
  • @jeroenh having the actual objects is unfortunately not a possibility. one implementation would have the age and surname and another implementation has location and temperature. Users can create a object as they need so the it is fully generic. The stored model is a dictionary of string, object. Commented Oct 2, 2020 at 9:23
  • @StephanBadenhorst why not make a common base class/interface that have common property for the target types? Commented Oct 2, 2020 at 9:26

1 Answer 1

4

This is easy, and you probably don't even need to know the type as it'll work it out for you:

// Given "input" is your Dictionary<string,Dictionary<string,object>>
var result = input.OrderBy(kv => kv.Value["Temp"]);

Live example: https://dotnetfiddle.net/au0vNI

Sign up to request clarification or add additional context in comments.

2 Comments

It's worth noting that result is no longer a dictionary and can't be used like a dictionary anymore. It's an enumerable type now. I'd also hazard a guess that this isn't very efficient from a Big O point of view..
@Liam yes - I pointed this out to the OP In comments (now deleted) and they updated their question with the expectation of an IEnumerable<KeyValuePair<.....>> which is what this returns. As for BigO - this is O1 to read the sub dict for each element in the upper dict which will be O(n). Not too bad I wouldnt have thought.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.