Reputation: 239
I am using dictionary to collect some information. After I collect the details I need to sort the dictionary values based on the key using var Variable. After that I need to insert the sorted var variable to the new dictionary. Here, I have trouble to insert the sorted data into dictionary.
I'm posting my code Here:
var orderDic = from dic in dictionaryColl
orderby dic.Key ascending
select dic;
Dictionary<int, string> newDic = new Dictionary<int, string>();
newDic =(Dictionary<int,string>)orderDic; // Here i'm not able to assign data to dictionary
Thanks in Advance..
Upvotes: 1
Views: 221
Reputation: 629
You can use
var _ordered = _dic.OrderBy(d => d.key).ToDictionary(d => d.Key, d => d.Value);
method to order the dictionary in ascending order.
or
you can use
var _ordered = _dic.OrderByDescending(d => d.key).ToDictionary(d => d.Key, d => d.Value);
method to order the dictionary in descending order.
Or else if you want to replace the same dictionary then
var _ordered = _dic.OrderByDescending(d => d.key).ToDictionary(d => d.Key, d => d.Value);
in case your original Dictionary is _dic
_dic=new Dictionary<int, string>(_ordered);
Please go through the following code,
class Program
{
static void Main(string[] args)
{
Dictionary<int,string> _dic=new Dictionary<int,string>();
_dic.Add(5,"xyz");
_dic.Add(2,"abc");
_dic.Add(1,"pqr");
_dic.Add(4,"test");
_dic.Add(3,"pvf");
var _ordered=_dic.OrderBy(d => d.Key).ToDictionary(d=> d.Key,d=>d.Value);
_dic=new Dictionary<int,string>(_ordered);
}
}
You can debug the above application and check that _dic is having the sorted dictionary based on the key.
Upvotes: -2
Reputation: 455
newDic =(Dictionary<int,string>).OrderBy(l => l.ColName).ToList();
Where ColName
is name of column you want to apply sorting or ordering.
Upvotes: 0
Reputation: 223267
You can directly convert it to dictionary like:
Dictionary<int, string> newDic = (from dic in dictionaryColl
orderby dic.Key ascending
select dic)
.ToDictionary(r => r.Key, r => r.Value);
Or with method syntax:
Dictionary<int, string> newDic = dictionaryColl
.OrderBy(r => r.Key)
.ToDictionary(r => r.Key, r => r.Value);
EDIT: OrderBy
would not effect the retrieval of Dictionary items in order.
See: Dictionary<TKey, TValue> Class
For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair structure representing a value and its key. The order in which the items are returned is undefined.
What you need is SortedDictionary like:
SortedDictionary<int, string> sortedNewDic =
new SortedDictionary<int, string>(dictionaryColl);
Upvotes: 4
Reputation: 2192
Why don't you use SortedDictionary at first place (http://msdn.microsoft.com/en-us/library/f7fta44c.aspx) ?
Upvotes: 0
Reputation: 18474
You just need
newDic = orderDic.ToDictionary(x=> x.Key, x => x.Value);
However this will not really help much as the order by clause won't affect the dictionary order.
It's possible you want a SortedDictionary?
new Dic = new SortedDictionary(dictionaryColl);
Upvotes: 3