Accepted answer

ok check this it should work

var r = new Dictionary<string, Point>();
r.Add("c3", new Point(0, 0));
r.Add("c1", new Point(0, 0));
r.Add("t3", new Point(0, 0));
r.Add("c4", new Point(0, 0));
r.Add("c2", new Point(0, 0));
r.Add("t1", new Point(0, 0));
r.Add("t2", new Point(0, 0));
var l = r.OrderBy(key => key.Key);
var dic = l.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value);

foreach (var item in dic)



I used

var l =  Input.OrderBy(key => key.Key);

and I converted it to Dictionary


The following code uses two more lists to sort a dictionary.

using System;
using System.Collections.Generic;
using System.Drawing;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            Dictionary<string,Point> r=new Dictionary<string,Point>();
            r.Add("c3",new Point(0,1));
            r.Add("c1",new Point(1,2));
            r.Add("t3",new Point(2,3));
            r.Add("c4",new Point(3,4));
            r.Add("c2",new Point(4,5));
            r.Add("t1",new Point(5,6));
            r.Add("t2",new Point(6,7));
            // Create a list of keys
            List<string> zlk=new List<string>(r.Keys);
            // and then sort it.
            List<Point> zlv=new List<Point>();
            // Readd with the order.
            foreach(var item in zlk) {
            for(int i=0;i<zlk.Count;i++) {
            // test output
            foreach(var item in r.Keys) {
                Console.WriteLine(item+" "+r[item].X+" "+r[item].Y);

The output of the code above is shown below.

c1 1 2
c2 4 5
c3 0 1
c4 3 4
t1 5 6
t2 6 7
t3 2 3


Just a guess but it looks like you are assuming it is going to sort Input. The OrderBy method actually returns an ordered instance of an IOrderedEnumerable containing the same values. If you want to keep the return value you can do the below:

IOrderedEnumerable orderedInput
orderedInput = Input.OrderBy(key=>key.Key)

Most methods that would modify the collection follow this same pattern. It does this so that it is not changing the origional collection instance. This protects you from accidently changing the instance when you didn't intend to. If you do want to only use the sorted instance then you just set the variable to the return of the method as shown above.


Since Input.OrderBy creates a query that returns the items in an ordered order, just assign it to the same dictionary.

objectDict = objectDict.OrderBy(obj => obj.Key).ToDictionary(obj => obj.Key, obj => obj.Value);


Load the unsorted object into a SortedDictionary object like so:

SortedDictionary<string, string> sortedCustomerData = new SortedDictionary<string,string>(unsortedCustomerData);

Where unsortedCustomerData is the same generic type (Dictionary string, string or in your case string, point). It will automatically sort the new object by key

According to msdn: SortedDictionary(IDictionary): Initializes a new instance of the SortedDictionary class that contains elements copied from the specified IDictionary and uses the default IComparer implementation for the key type.


Input.OrderBy does not sort the dictionary, it creates a query that returns the items in a specific order.

Perhaps OrderedDictionary gives you what you want.

Or use the Generic SortedDictionary

Related Query

More Query from same tag