3

I have this list :

List<Dictionary<int, string>> list = new List<Dictionary<int, string>>(); 

and I'd like to order it (ascending) based on Dictionary int value. How can I do it?

Example :

Dictionary<int, string> Min1 = new Dictionary<int, string>(); Dictionary<int, string> Min2 = new Dictionary<int, string>(); Dictionary<int, string> Min3 = new Dictionary<int, string>(); Dictionary<int, string> Min4 = new Dictionary<int, string>(); Dictionary<int, string> Min5 = new Dictionary<int, string>(); Min1.Add(3, "name"); Min2.Add(1, "hello"); Min3.Add(5, "marco"); Min4.Add(4, "is"); Min5.Add(2, "my"); List<Dictionary<int, string>> list = new List<Dictionary<int, string>>(); list.Add(Min1); list.Add(Min2); list.Add(Min3); list.Add(Min4); list.Add(Min5); // NOW I NEED TO ORDER list BASED ON DICTIONARY INT VALUE // SO THE ORDER ON THE LIST SHOULD BE Min2, Min5, Min1, Min4, Min3 

I'd like to sort the list, so when I cycle it and I get to the string I print "hello my name is marco"

12
  • 2
    can you give an example of sorting you want? Commented Dec 28, 2011 at 11:09
  • 1
    This doesn't make much sense. Do you want to order by the smallest key in each dictionary? You have to have one value to compare different dictionaries and need to choose which one it is. Commented Dec 28, 2011 at 11:12
  • 1
    You could use the SortedList class Commented Dec 28, 2011 at 11:16
  • Your sample doesn't show relation between your dictionaries and list, what you said dictionary in your sample is just keyValue pair not dictionary. Commented Dec 28, 2011 at 11:17
  • Still your dictionaries are not good at least have one dictionary of size 2 and show what you want, In fact in current sample you just need KeyValue pair not dicionaries, in fact all of them can be in one dictionary. Commented Dec 28, 2011 at 11:32

4 Answers 4

6

if you want to flatten data and sort them you can try this:

list.SelectMany(x=>x).OrderBy(x=>x.Key) 

but if you don't like flatten data and just sort dictionaries you can do:

list.Select(x=>x.OrderBy(y=>y.Key)); 

Edit: As I understand you just need use one dictionary, so you can do this:

Dictionary<int,string> dic = new Dictionary<int,string>(); var result = dic.OrderBy(x=>x.Key).ToLookup(x=>x); 

If you want use List instead of dictionary (in your case seems is not good) you can do this;

 List<KeyValuePair<int,string>> lst1 = new List<KeyValuePair<int,string>>(); var result = lst1.OrderBy(x=>x.Key).ToLookup(x=>x); 
Sign up to request clarification or add additional context in comments.

Comments

1

You Can make use of SortedDictionary or SortedList when you define the Generics. When you do that, no matter in what sequence you add elements to your Dictionary, the elements will be Stored in a sorted format based on the key-element(Which is of int Type in your case). Additionally SortedDictionary uses BinarySearch tree, which makes it more time efficient.

1 Comment

One more thing, I wonder why do you use 5 Dictionaries, while a single dictionary (min1) can store all your KeyValue Pairs.
1

Once you have a single dictionary, then you can create an ordered list of the values based on the keys in the dictionary as follows using LINQ:

var dict = new List<Dictionary<int, string>>(); var list = dict.OrderBy(e => e.Key).ToList(); 

Without LINQ you can use a SortedList:

SortedList<int, string> sortList = new SortedList<int, string>(dict); 

From this, if you want a plain List<T> then:

List<string> list = new List<string>(sortList.Values); 

Comments

1

However Marco, I took the above scenario as a challenge and made a code-implementation of the result that you want. I am posting the code below. In a nut-shell you have to make another list which stores the sorted dictionary elements. You can run the following code in your debugger and see the result on your own.

 public class program { public static void Main() { SortedDictionary<int, string> Min1 = new SortedDictionary<int, string>(); SortedDictionary<int, string> Min2 = new SortedDictionary<int, string>(); SortedDictionary<int, string> Min3 = new SortedDictionary<int, string>(); SortedDictionary<int, string> Min4 = new SortedDictionary<int, string>(); SortedDictionary<int, string> Min5 = new SortedDictionary<int, string>(); Min1.Add(3, "name"); Min2.Add(1, "hello"); Min3.Add(5, "marco"); Min4.Add(4, "is"); Min5.Add(2, "my"); List<SortedDictionary<int, string>> list = new List<SortedDictionary<int, string>>(); list.Add(Min1); list.Add(Min2); list.Add(Min3); list.Add(Min4); list.Add(Min5); List<SortedDictionary<int, string>> final_list = new List<SortedDictionary<int, string>>(); List<int> index = new List<int>() ; foreach (var element in list) { foreach (var elements in element) { index.Add(elements.Key); Console.Write(" " +elements.Value+ " "); } } index.Sort(); foreach (var indexelement in index) { foreach (var element in list) { foreach (var elements in element) { if (indexelement == elements.Key) { final_list.Add(element); } } } } Console.WriteLine(); foreach (var element in final_list) { foreach (var elements in element) { Console.Write(" " + elements.Value+ " "); } } Console.ReadLine(); } } 

Happy coding :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.