0

I have a dictionary that contains another dictionary as value something like this

Dictionary<string, Dictionary<double,double>> 

now I want to sort it by internal dictionary's value how can I do that??

6
  • 2
    I'm not even going to imagine why you would need such a data structure, but what are some examples of data you would be holding, and how are you trying to sort the data? Remember that a dictionary itself is not sortable. Commented Jun 2, 2013 at 1:05
  • Some values of dictionaries and on what basis you want to sort them would help. Commented Jun 2, 2013 at 1:08
  • sorry i am not expert on c#, would you suggest a way to store 3 values as one item? Commented Jun 2, 2013 at 1:10
  • 2
    @user1225072: Create a class having 3 properties. One string and other two double. Commented Jun 2, 2013 at 1:11
  • let me try and get back to u Commented Jun 2, 2013 at 1:13

2 Answers 2

4

From the looks of your comment

sorry i am not expert on c#, would you suggest a way to store 3 values as one item?

I would suggest creating a class and sort on it like this

public class MyClass
{
    public string StringProperty {get;set;}
    public int FirstDoubleProperty {get;set;}
    public int SecondDoubleProperty {get;set;}
}

Then create a collection like this

List<MyClass> MyClasscol = new List<MyClass>();

MyClass mc = new MyClass();

mc.StringProperty = "User1225072";
mc.FirstDoubleProperty = 5;
mc.SecondDoubleProperty = 6;

MyClasscol.Add(mc);

mc = new MyClass();

// and So on

then sort like this

var newsortedcollection = MyClasscol.OrderBy(x => x.FirstDoubleProperty);
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you are now trying to figure out how to store and order a collection of objects with multiple properties, then you have a few options. Nikhil Agrawal's answer is a great solution but there are times when you may not need/want to create a custom class for this. For these situations (preferably when your code is private and not part of some API) then the alternatives below might be an option.

KeyValuePairs

Based on your requirements and your original post using dictionaries, it seems like instead of a dictionary of dictionaries (multi-tiered), you probably wanted a dictionary of keyvaluepairs (flat).

// using keyvaluepair
var keyValueDict = new Dictionary<string, KeyValuePair<double, double>>();
keyValueDict.Add("string", new KeyValuePair<double, double>(5.8, 7.4));
var sortedKeyValues = keyValueDict.OrderBy(x => x.Value.Key);

Tuples

An alternative to the not so pleasant KeyValuePair is the Tuple introduced in .NET 4. The tuple is a generic class which allows you to store typed property values without creating your own custom class. It is worth noting that there are tuple implementations for up to 8 properties.

// using tuple
var tupleList = new List<Tuple<string, double, double>>();
tupleList.Add(new Tuple<string, double, double>("string", 5.8, 7.4));
var sortedTuples = tupleList.OrderBy(x => x.Item2);

There are some good SO questions about Tuples if you are interested:
Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?
Are EventArg classes needed now that we have generics

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.