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
Dictionaries are made up of key: value pairs. Thus, they can be sorted by the keys or by the values.
To sort dictionary key in python we can use dict. items() and sorted(iterable) method. Dict. items() method returns an object that stores key-value pairs of dictionaries.
To sort a list of dictionaries according to the value of the specific key, specify the key parameter of the sort() method or the sorted() function. By specifying a function to be applied to each element of the list, it is sorted according to the result of that function.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With