How to sort a dictionary by key

I have dictionary Dictionary<string, Point>

the Key is c1,c3,c2,t1,,t4,t2 I want to sort them to be c1,c2,c3,t1,t2,t3

I'm trying to sort it using

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

but it doesn't work

any idea how to solve that

Answers


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)
{

    Console.WriteLine(item.Key);
}
Console.ReadLine();

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

Perhaps OrderedDictionary gives you what you want.

Or use the Generic SortedDictionary


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.


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);


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.
            zlk.Sort();
            List<Point> zlv=new List<Point>();
            // Readd with the order.
            foreach(var item in zlk) {
                zlv.Add(r[item]);
            }
            r.Clear();
            for(int i=0;i<zlk.Count;i++) {
                r[zlk[i]]=zlv[i];
            }
            // test output
            foreach(var item in r.Keys) {
                Console.WriteLine(item+" "+r[item].X+" "+r[item].Y);
            }
            Console.ReadKey(true);
        }
    }
}

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

I used

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

and I converted it to Dictionary


Need Your Help

What is the best way to get an element's tag name in JS?

javascript html prototypejs

I want to get the name of a certain tag (to tell whether it is a div/input/span/p/so on)

Android - Playing mp3 from byte[]

android bytearray mp3 audio-player

I have my mp3 file in byte[] (downloaded from an service) and I would like to play it on my device similar to how you can play files: